Skip to content

Commit a033288

Browse files
committed
illutrate how to combine clock/calendar and dialog host
1 parent 3acfa90 commit a033288

File tree

8 files changed

+201
-12
lines changed

8 files changed

+201
-12
lines changed

MainDemo.Wpf/App.xaml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
<Application x:Class="MaterialDesignColors.WpfExample.App"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4-
xmlns:system="clr-namespace:System;assembly=mscorlib"
5-
xmlns:wpf="clr-namespace:MaterialDesignThemes.Wpf;assembly=MaterialDesignThemes.Wpf"
64
StartupUri="MainWindow.xaml">
75
<Application.Resources>
86
<ResourceDictionary>
97
<ResourceDictionary.MergedDictionaries>
10-
11-
<!-- Material -->
8+
129
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
1310
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
1411
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />

MainDemo.Wpf/MainDemo.Wpf.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@
116116
<DependentUpon>PaletteSelector.xaml</DependentUpon>
117117
</Compile>
118118
<Compile Include="PaletteSelectorViewModel.cs" />
119+
<Compile Include="Pickers.xaml.cs">
120+
<DependentUpon>Pickers.xaml</DependentUpon>
121+
</Compile>
122+
<Compile Include="PickersViewModel.cs" />
119123
<Compile Include="Progress.xaml.cs">
120124
<DependentUpon>Progress.xaml</DependentUpon>
121125
</Compile>
@@ -202,6 +206,10 @@
202206
<SubType>Designer</SubType>
203207
<Generator>MSBuild:Compile</Generator>
204208
</Page>
209+
<Page Include="Pickers.xaml">
210+
<SubType>Designer</SubType>
211+
<Generator>MSBuild:Compile</Generator>
212+
</Page>
205213
<Page Include="Progress.xaml">
206214
<SubType>Designer</SubType>
207215
<Generator>MSBuild:Compile</Generator>

MainDemo.Wpf/MainWindow.xaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@
7070
<wpfExample:TextFields />
7171
</domain:DemoItem.Content>
7272
</domain:DemoItem>
73+
<domain:DemoItem Name="Pickers">
74+
<domain:DemoItem.Content>
75+
<wpfExample:Pickers>
76+
<wpfExample:Pickers.DataContext>
77+
<wpfExample:PickersViewModel />
78+
</wpfExample:Pickers.DataContext>
79+
</wpfExample:Pickers>
80+
</domain:DemoItem.Content>
81+
</domain:DemoItem>
7382
<domain:DemoItem Name="Sliders">
7483
<domain:DemoItem.Content>
7584
<wpfExample:Sliders />

MainDemo.Wpf/Pickers.xaml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<UserControl x:Class="MaterialDesignColors.WpfExample.Pickers"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
7+
mc:Ignorable="d"
8+
d:DesignHeight="300" d:DesignWidth="300">
9+
<Grid Margin="32">
10+
<Grid.RowDefinitions>
11+
<RowDefinition Height="Auto" />
12+
<RowDefinition Height="Auto" />
13+
<RowDefinition Height="Auto" />
14+
<RowDefinition Height="Auto" />
15+
</Grid.RowDefinitions>
16+
<Grid.ColumnDefinitions>
17+
<ColumnDefinition Width="Auto" />
18+
<ColumnDefinition Width="Auto" />
19+
<ColumnDefinition Width="Auto" />
20+
</Grid.ColumnDefinitions>
21+
<TextBlock Grid.ColumnSpan="3">Classic WPF DatePicker control with Material Design theme, and new TimePicker control:</TextBlock>
22+
<DatePicker Grid.Row="1" Grid.Column="0" Width="100" HorizontalAlignment="Left" Margin="0 16 0 0"
23+
materialDesign:TextFieldAssist.Hint="Pick Date"/>
24+
<materialDesign:TimePicker Grid.Row="1" Grid.Column="1" VerticalAlignment="Top" Width="100" HorizontalAlignment="Left" Margin="0 16 0 0"
25+
materialDesign:TextFieldAssist.Hint="Custom hint" />
26+
<materialDesign:TimePicker Grid.Row="1" Grid.Column="2" Is24Hours="True" x:Name="PresetTimePicker" VerticalAlignment="Top" Width="100" HorizontalAlignment="Left" Margin="0 16 0 0" />
27+
28+
<TextBlock Grid.Row="2" Grid.ColumnSpan="3" Margin="0 128 0 0">By combining the DialogHost (see MainWindow.xaml) and the Calendar and Clock controls, custom popups can be built.</TextBlock>
29+
<StackPanel Grid.Row="3" Grid.Column="0" Orientation="Horizontal" Margin="0 32 0 0">
30+
<TextBlock Text="{Binding Date, StringFormat=d}" VerticalAlignment="Center" FontSize="24" />
31+
<Button Margin="8 0 0 0" Content="..."
32+
Command="{x:Static materialDesign:DialogHost.OpenDialogCommand}"
33+
materialDesign:DialogHost.DialogOpenedAttached="CalendarDialogOpenedEventHandler"
34+
materialDesign:DialogHost.DialogClosingAttached="CalendarDialogClosingEventHandler">
35+
<Button.CommandParameter>
36+
<Grid>
37+
<Grid.RowDefinitions>
38+
<RowDefinition Height="Auto" />
39+
<RowDefinition Height="Auto" />
40+
</Grid.RowDefinitions>
41+
<Calendar x:Name="Calendar" Margin="-1 -4 -1 0" />
42+
<StackPanel Grid.Row="1" Margin="8" HorizontalAlignment="Right" Orientation="Horizontal">
43+
<Button Command="{x:Static materialDesign:DialogHost.CloseDialogCommand}" CommandParameter="1" Style="{DynamicResource MaterialDesignFlatButton}">OK</Button>
44+
<Button Command="{x:Static materialDesign:DialogHost.CloseDialogCommand}" CommandParameter="0" Style="{DynamicResource MaterialDesignFlatButton}">CANCEL</Button>
45+
</StackPanel>
46+
</Grid>
47+
</Button.CommandParameter>
48+
</Button>
49+
</StackPanel>
50+
<StackPanel Grid.Row="3" Grid.Column="1" Orientation="Horizontal" Margin="0 32 0 0">
51+
<TextBlock Text="{Binding Time, StringFormat=t}" VerticalAlignment="Center" FontSize="24" />
52+
<Button Margin="8 0 0 0" Content="..."
53+
Command="{x:Static materialDesign:DialogHost.OpenDialogCommand}"
54+
materialDesign:DialogHost.DialogOpenedAttached="ClockDialogOpenedEventHandler"
55+
materialDesign:DialogHost.DialogClosingAttached="ClockDialogClosingEventHandler">
56+
<Button.CommandParameter>
57+
<Grid Margin="-1">
58+
<Grid.RowDefinitions>
59+
<RowDefinition Height="Auto" />
60+
<RowDefinition Height="Auto" />
61+
</Grid.RowDefinitions>
62+
<materialDesign:Clock DisplayAutomation="Cycle" x:Name="Clock" />
63+
<StackPanel Grid.Row="1" Margin="8" HorizontalAlignment="Right" Orientation="Horizontal">
64+
<Button Command="{x:Static materialDesign:DialogHost.CloseDialogCommand}" CommandParameter="1" Style="{DynamicResource MaterialDesignFlatButton}">OK</Button>
65+
<Button Command="{x:Static materialDesign:DialogHost.CloseDialogCommand}" CommandParameter="0" Style="{DynamicResource MaterialDesignFlatButton}">CANCEL</Button>
66+
</StackPanel>
67+
</Grid>
68+
</Button.CommandParameter>
69+
</Button>
70+
</StackPanel>
71+
72+
73+
74+
</Grid>
75+
</UserControl>

MainDemo.Wpf/Pickers.xaml.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Windows;
7+
using System.Windows.Controls;
8+
using System.Windows.Data;
9+
using System.Windows.Documents;
10+
using System.Windows.Input;
11+
using System.Windows.Media;
12+
using System.Windows.Media.Imaging;
13+
using System.Windows.Navigation;
14+
using System.Windows.Shapes;
15+
using MaterialDesignThemes.Wpf;
16+
17+
namespace MaterialDesignColors.WpfExample
18+
{
19+
/// <summary>
20+
/// Interaction logic for Pickers.xaml
21+
/// </summary>
22+
public partial class Pickers : UserControl
23+
{
24+
public Pickers()
25+
{
26+
InitializeComponent();
27+
}
28+
29+
public void CalendarDialogOpenedEventHandler(object sender, DialogOpenedEventArgs eventArgs)
30+
{
31+
Calendar.SelectedDate = ((PickersViewModel)DataContext).Date;
32+
}
33+
34+
public void CalendarDialogClosingEventHandler(object sender, DialogClosingEventArgs eventArgs)
35+
{
36+
if (!Equals(eventArgs.Parameter, "1")) return;
37+
38+
if (!Calendar.SelectedDate.HasValue)
39+
{
40+
eventArgs.Cancel();
41+
return;
42+
}
43+
44+
((PickersViewModel)DataContext).Date = Calendar.SelectedDate.Value;
45+
}
46+
47+
public void ClockDialogOpenedEventHandler(object sender, DialogOpenedEventArgs eventArgs)
48+
{
49+
Clock.Time = ((PickersViewModel) DataContext).Time;
50+
}
51+
52+
public void ClockDialogClosingEventHandler(object sender, DialogClosingEventArgs eventArgs)
53+
{
54+
if (Equals(eventArgs.Parameter, "1"))
55+
((PickersViewModel)DataContext).Time = Clock.Time;
56+
}
57+
}
58+
}

MainDemo.Wpf/PickersViewModel.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel;
4+
using System.Linq;
5+
using System.Runtime.CompilerServices;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace MaterialDesignColors.WpfExample
10+
{
11+
public class PickersViewModel : INotifyPropertyChanged
12+
{
13+
private DateTime _date;
14+
private DateTime _time;
15+
16+
public PickersViewModel()
17+
{
18+
Date = DateTime.Now;
19+
Time = DateTime.Now;
20+
}
21+
22+
public DateTime Date
23+
{
24+
get { return _date; }
25+
set
26+
{
27+
_date = value;
28+
OnPropertyChanged();
29+
}
30+
}
31+
32+
public DateTime Time
33+
{
34+
get { return _time; }
35+
set
36+
{
37+
_time = value;
38+
OnPropertyChanged();
39+
}
40+
}
41+
42+
public event PropertyChangedEventHandler PropertyChanged;
43+
44+
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
45+
{
46+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
47+
}
48+
}
49+
}

MainDemo.Wpf/TextFields.xaml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
<RowDefinition Height="Auto" />
5151
<RowDefinition Height="Auto" />
5252
<RowDefinition Height="Auto" />
53-
<RowDefinition Height="Auto" />
5453
</Grid.RowDefinitions>
5554
<Viewbox Grid.Row="0" Grid.Column="0">
5655
<Canvas Width="24" Height="24">
@@ -172,10 +171,5 @@
172171
<ComboBoxItem>Orange</ComboBoxItem>
173172
</ComboBox>
174173

175-
<DatePicker Grid.Row="5" Grid.Column="1" Width="100" HorizontalAlignment="Left" Margin="0 8 0 8"
176-
materialDesign:TextFieldAssist.Hint="Pick Date"/>
177-
<materialDesign:TimePicker Grid.Row="5" Grid.Column="3" VerticalAlignment="Top" Width="100" HorizontalAlignment="Left" Margin="0 8 0 8"
178-
materialDesign:TextFieldAssist.Hint="Custom hint" />
179-
<materialDesign:TimePicker Grid.Row="5" Grid.Column="4" Is24Hours="True" x:Name="PresetTimePicker" VerticalAlignment="Top" Width="100" HorizontalAlignment="Left" Margin="0 8 0 8" />
180174
</Grid>
181175
</UserControl>

MainDemo.Wpf/TextFields.xaml.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ public TextFields()
3131
}
3232

3333
private void UserControl_Loaded(object sender, RoutedEventArgs e)
34-
{
35-
PresetTimePicker.SelectedTime = DateTime.Now.Date.AddHours(13).AddMinutes(37);
34+
{
3635
}
3736
}
3837

0 commit comments

Comments
 (0)