Skip to content

Instantly share code, notes, and snippets.

@fatihbahceci
Created October 31, 2021 13:33
Show Gist options
  • Save fatihbahceci/72058be6fd886719f723bd6c4b1b3d83 to your computer and use it in GitHub Desktop.
Save fatihbahceci/72058be6fd886719f723bd6c4b1b3d83 to your computer and use it in GitHub Desktop.
Avalonia DateTimePicker
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:AvaloniaTest"
xmlns:v="using:AvaloniaTest.Views"
x:Class="AvaloniaTest.App">
<Application.DataTemplates>
<local:ViewLocator/>
</Application.DataTemplates>
<Application.Styles>
<FluentTheme Mode="Light"/>
<Style Selector="v|FBCDateTimePickerPresenter" >
<Setter Property="Template">
<ControlTemplate>
<Grid ColumnDefinitions="Auto, Auto" RowDefinitions="Auto,40" Background="White">
<Grid.Styles>
<Style Selector="Grid" >
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
<Style Selector="Calendar">
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="Margin" Value="10"/>
</Style>
<Style Selector="NumericUpDown">
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="BorderBrush" Value="Orange"/>
<Setter Property="Margin" Value="0,10,10,0"/>
</Style>
<Style Selector="TextBlock" >
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Bottom"/>
<Setter Property="Margin" Value="0,10,0,0"/>
</Style>
<Style Selector="Button" >
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="BorderBrush" Value="Orange"/>
</Style>
</Grid.Styles>
<Calendar SelectedDate="{TemplateBinding DateOnly}"/>
<Grid Grid.Column="1" RowDefinitions="1*,1*,1*,1*,1*,1*">
<TextBlock Grid.Row="0" Text="Saat" />
<NumericUpDown Name="Hour" Grid.Row="1" Minimum="0" Maximum="23" Value="{TemplateBinding Hour}"/>
<TextBlock Grid.Row="2" Text="Dakika" />
<NumericUpDown Name="Minute" Grid.Row="3" Minimum="0" Maximum="59" Value="{TemplateBinding Minute}"/>
<TextBlock Grid.Row="4" Text="Saniye" />
<NumericUpDown Name="Second" Grid.Row="5" Minimum="0" Maximum="59" Value="{TemplateBinding Second}"/>
</Grid>
<Grid Grid.Row="2" Grid.ColumnSpan="2" ColumnDefinitions="1*, 1*">
<Button Grid.Column="0" Content="Tamam" Name="BtnOK"/>
<Button Grid.Column="1" Content="Vazgeç" Name="BtnCancel"/>
</Grid>
</Grid>
</ControlTemplate>
</Setter>
</Style>
</Application.Styles>
</Application>
using Avalonia;
using Avalonia.Controls.Primitives;
using System;
using Avalonia.Interactivity;
using Avalonia.Controls;
namespace AvaloniaTest.Views
{
/// <summary>
/// FBC: My references
/// https://github.com/AvaloniaUI/Avalonia/blob/master/src/Avalonia.Controls/DateTimePickers/DatePicker.cs
/// https://github.com/AvaloniaUI/Avalonia/blob/master/src/Avalonia.Themes.Default/DatePicker.xaml
/// https://github.com/AvaloniaUI/Avalonia/blob/master/src/Avalonia.Controls/DateTimePickers/DatePickerPresenter.cs
/// </summary>
public class FBCDateTimePickerPresenter : PickerPresenterBase
{
public FBCDateTimePickerPresenter(): base()
{
Value = DateTime.Now;
}
public static readonly DirectProperty<FBCDateTimePickerPresenter, DateTime> DateOnlyProperty =
AvaloniaProperty.RegisterDirect<FBCDateTimePickerPresenter, DateTime>(nameof(DateOnly),
x => x.DateOnly, (x, v) => x.DateOnly = v);
public static readonly DirectProperty<FBCDateTimePickerPresenter, int> HourProperty =
AvaloniaProperty.RegisterDirect<FBCDateTimePickerPresenter, int>(nameof(Hour),
x => x.Hour, (x, v) => x.Hour = v);
public static readonly DirectProperty<FBCDateTimePickerPresenter, int> MinuteProperty =
AvaloniaProperty.RegisterDirect<FBCDateTimePickerPresenter, int>(nameof(Minute),
x => x.Minute, (x, v) => x.Minute = v);
public static readonly DirectProperty<FBCDateTimePickerPresenter, int> SecondProperty =
AvaloniaProperty.RegisterDirect<FBCDateTimePickerPresenter, int>(nameof(Second),
x => x.Second, (x, v) => x.Second = v);
private DateTime dateOnly;
private int hour;
private int minute;
private int second;
public DateTime DateOnly
{
get { return dateOnly; }
set
{
SetAndRaise(DateOnlyProperty, ref dateOnly, value);
}
}
public int Hour
{
get { return hour; }
set
{
SetAndRaise(HourProperty, ref hour, value);
}
}
public int Minute
{
get { return minute; }
set
{
minute = value;
SetAndRaise(MinuteProperty, ref minute, value);
}
}
public int Second
{
get { return second; }
set
{
second = value;
SetAndRaise(SecondProperty, ref second, value);
}
}
NumericUpDown nuHour;
NumericUpDown nuMinute;
NumericUpDown nuSecond;
Button BtnOK;
Button BtnCancel;
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
nuHour = e.NameScope.Get<NumericUpDown>("Hour");
nuMinute = e.NameScope.Get<NumericUpDown>("Minute");
nuSecond = e.NameScope.Get<NumericUpDown>("Second");
BtnOK = e.NameScope.Get<Button>("BtnOK");
BtnCancel = e.NameScope.Get<Button>("BtnCancel");
if (BtnOK != null)
{
BtnOK.Click += OnAcceptButtonClicked;
}
if (BtnCancel != null)
{
BtnCancel.Click += OnDismissButtonClicked;
}
}
private void OnDismissButtonClicked(object? sender, RoutedEventArgs e)
{
OnDismiss();
}
private void OnAcceptButtonClicked(object? sender, RoutedEventArgs e)
{
//Date = _syncDate;
OnConfirmed();
}
public DateTime Value
{
get { return new DateTime(DateOnly.Year, DateOnly.Month, DateOnly.Day, Hour, Minute, Second); }
set
{
DateOnly = value.Date;
Hour = value.Hour;
Minute = value.Minute;
Second = value.Second;
//OnPropertyChanged();
}
}
}
}
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:AvaloniaTest.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="AvaloniaTest.Views.MainWindow"
Icon="/Assets/avalonia-logo.ico"
xmlns:v="using:AvaloniaTest.Views"
Title="AvaloniaTest">
<Design.DataContext>
<vm:MainWindowViewModel/>
</Design.DataContext>
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<Button Content="Popeye" Name="sabri"/>
<Popup Name="Popup"
WindowManagerAddShadowHint="False"
StaysOpen="False"
PlacementMode="Bottom"
PlacementTarget="{Binding ElementName=sabri}">
<v:FBCDateTimePickerPresenter Name="PopPresenter"/>
</Popup>
</StackPanel>
</Window>
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Markup.Xaml;
namespace AvaloniaTest.Views
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
#if DEBUG
this.AttachDevTools();
#endif
}
Button sabri;
Popup pop;
FBCDateTimePickerPresenter popPresenter;
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
sabri = this.FindControl<Button>("sabri");
pop = this.FindControl<Popup>("Popup");
popPresenter = this.FindControl<FBCDateTimePickerPresenter>("PopPresenter");
popPresenter.Confirmed += PopPresenter_Confirmed;
popPresenter.Dismissed += PopPresenter_Dismissed;
sabri.Tapped += Sabri_Tapped;
}
private void PopPresenter_Dismissed(object? sender, System.EventArgs e)
{
pop.Close();
sabri.Content = "Cancelled";
}
private void PopPresenter_Confirmed(object? sender, System.EventArgs e)
{
pop.Close();
sabri.Content = "New value is:" + popPresenter.Value;
}
private void Sabri_Tapped(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
pop.IsOpen = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment