Skip to content

Commit dd269a7

Browse files
committed
Added TimePicker.IsInvalidTextAllowed to allow data error templates to display. And added the error templates. Fixes #325
1 parent b621a52 commit dd269a7

13 files changed

+167
-78
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Windows.Controls;
4+
5+
namespace MaterialDesignDemo.Domain
6+
{
7+
public class FutureDateValidationRule : ValidationRule
8+
{
9+
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
10+
{
11+
DateTime time;
12+
if (!DateTime.TryParse((value ?? "").ToString(),
13+
CultureInfo.CurrentCulture,
14+
DateTimeStyles.AssumeLocal | DateTimeStyles.AllowWhiteSpaces,
15+
out time)) return new ValidationResult(false, "Invalid date");
16+
17+
return time.Date <= DateTime.Now.Date
18+
? new ValidationResult(false, "Future date required")
19+
: ValidationResult.ValidResult;
20+
}
21+
}
22+
}

MainDemo.Wpf/NotEmptyValidationRule.cs renamed to MainDemo.Wpf/Domain/NotEmptyValidationRule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Globalization;
22
using System.Windows.Controls;
33

4-
namespace MaterialDesignDemo
4+
namespace MaterialDesignDemo.Domain
55
{
66
public class NotEmptyValidationRule : ValidationRule
77
{

MainDemo.Wpf/Domain/SampleDialog.xaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
xmlns:wpf="clr-namespace:MaterialDesignThemes.Wpf;assembly=MaterialDesignThemes.Wpf"
77
xmlns:system="clr-namespace:System;assembly=mscorlib"
88
xmlns:materialDesignDemo="clr-namespace:MaterialDesignDemo"
9+
xmlns:domain="clr-namespace:MaterialDesignDemo.Domain"
910
mc:Ignorable="d"
1011
d:DesignHeight="300" d:DesignWidth="300">
1112
<Grid Margin="16">
@@ -23,7 +24,7 @@
2324
<TextBox.Text>
2425
<Binding Path="Name" UpdateSourceTrigger="PropertyChanged">
2526
<Binding.ValidationRules>
26-
<materialDesignDemo:NotEmptyValidationRule ValidatesOnTargetUpdated="True" />
27+
<domain:NotEmptyValidationRule ValidatesOnTargetUpdated="True" />
2728
</Binding.ValidationRules>
2829
</Binding>
2930
</TextBox.Text>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Windows.Controls;
4+
5+
namespace MaterialDesignDemo.Domain
6+
{
7+
public class SimpleDateValidationRule : ValidationRule
8+
{
9+
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
10+
{
11+
DateTime time;
12+
return DateTime.TryParse((value ?? "").ToString(),
13+
CultureInfo.CurrentCulture,
14+
DateTimeStyles.AssumeLocal | DateTimeStyles.AllowWhiteSpaces,
15+
out time)
16+
? ValidationResult.ValidResult
17+
: new ValidationResult(false, "Invalid date");
18+
}
19+
}
20+
}

MainDemo.Wpf/MaterialDesignDemo.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@
8686
<DependentUpon>Dialogs.xaml</DependentUpon>
8787
</Compile>
8888
<Compile Include="Domain\AnotherCommandImplementation.cs" />
89+
<Compile Include="Domain\FutureDateValidationRule.cs" />
90+
<Compile Include="Domain\NotEmptyValidationRule.cs" />
8991
<Compile Include="Domain\NotifyPropertyChangedExtension.cs" />
9092
<Compile Include="Domain\DemoItem.cs" />
9193
<Compile Include="Domain\DialogsViewModel.cs" />
@@ -105,6 +107,7 @@
105107
</Compile>
106108
<Compile Include="Domain\SelectableViewModel.cs" />
107109
<Compile Include="Domain\SimpleDataTemplateSelector.cs" />
110+
<Compile Include="Domain\SimpleDateValidationRule.cs" />
108111
<Compile Include="Domain\TextFieldsViewModel.cs" />
109112
<Compile Include="Domain\TreesViewModel.cs" />
110113
<Compile Include="Expander.xaml.cs">
@@ -126,7 +129,6 @@
126129
<Compile Include="MenusAndToolBars.xaml.cs">
127130
<DependentUpon>MenusAndToolBars.xaml</DependentUpon>
128131
</Compile>
129-
<Compile Include="NotEmptyValidationRule.cs" />
130132
<Compile Include="PaletteSelector.xaml.cs">
131133
<DependentUpon>PaletteSelector.xaml</DependentUpon>
132134
</Compile>

MainDemo.Wpf/Pickers.xaml

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
55
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
66
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
7+
xmlns:domain="clr-namespace:MaterialDesignDemo.Domain"
78
mc:Ignorable="d"
89
d:DesignHeight="300" d:DesignWidth="300">
9-
<Grid Margin="32">
10+
<!-- for some reason to get the validation templates to show on this page need an extra decorator. haven't figure out why yet -->
11+
<AdornerDecorator>
12+
<Grid Margin="32">
1013
<Grid.RowDefinitions>
1114
<RowDefinition Height="Auto" />
1215
<RowDefinition Height="Auto" />
@@ -17,16 +20,38 @@
1720
<ColumnDefinition Width="Auto" />
1821
<ColumnDefinition Width="Auto" />
1922
<ColumnDefinition Width="Auto" />
23+
<ColumnDefinition />
2024
</Grid.ColumnDefinitions>
2125
<TextBlock Grid.ColumnSpan="3">Classic WPF DatePicker control with Material Design theme, and new TimePicker control:</TextBlock>
2226
<StackPanel Grid.Row="1" Grid.Column="0">
2327
<DatePicker Width="100" HorizontalAlignment="Left" Margin="0 16 0 0" materialDesign:HintAssist.Hint="Pick Date" Style="{StaticResource MaterialDesignFloatingHintDatePicker}" />
24-
<DatePicker x:Name="FutureDatePicker" Width="100" HorizontalAlignment="Left" Margin="0 16 0 0" materialDesign:HintAssist.Hint="Future Date" />
28+
<DatePicker x:Name="FutureDatePicker" Width="100" HorizontalAlignment="Left" Margin="0 16 0 0"
29+
materialDesign:HintAssist.Hint="Future Date">
30+
<DatePicker.SelectedDate>
31+
<Binding Path="FutureValidatingDate" UpdateSourceTrigger="PropertyChanged">
32+
<Binding.ValidationRules>
33+
<domain:FutureDateValidationRule ValidatesOnTargetUpdated="True" />
34+
</Binding.ValidationRules>
35+
</Binding>
36+
</DatePicker.SelectedDate>
37+
</DatePicker>
2538
</StackPanel>
2639
<materialDesign:TimePicker Grid.Row="1" Grid.Column="1" VerticalAlignment="Top" Width="100" HorizontalAlignment="Left" Margin="0 16 0 0"
2740
Style="{StaticResource MaterialDesignFloatingHintTimePicker}"
2841
materialDesign:HintAssist.Hint="Custom hint" />
2942
<materialDesign:TimePicker Grid.Row="1" Grid.Column="2" Is24Hours="True" x:Name="PresetTimePicker" VerticalAlignment="Top" Width="100" HorizontalAlignment="Left" Margin="0 16 0 0" />
43+
<materialDesign:TimePicker Grid.Row="1" Grid.Column="3"
44+
materialDesign:HintAssist.Hint="Validates"
45+
IsInvalidTextAllowed="True"
46+
Is24Hours="True" VerticalAlignment="Top" Width="100" HorizontalAlignment="Left" Margin="0 16 0 0">
47+
<materialDesign:TimePicker.Text>
48+
<Binding Path="ValidatingTime" UpdateSourceTrigger="PropertyChanged">
49+
<Binding.ValidationRules>
50+
<domain:SimpleDateValidationRule ValidatesOnTargetUpdated="True" />
51+
</Binding.ValidationRules>
52+
</Binding>
53+
</materialDesign:TimePicker.Text>
54+
</materialDesign:TimePicker>
3055

3156
<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>
3257
<StackPanel Grid.Row="3" Grid.Column="0" Orientation="Horizontal" Margin="0 32 0 0">
@@ -75,4 +100,5 @@
75100

76101

77102
</Grid>
103+
</AdornerDecorator>
78104
</UserControl>

MainDemo.Wpf/PickersViewModel.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public class PickersViewModel : INotifyPropertyChanged
1212
{
1313
private DateTime _date;
1414
private DateTime _time;
15+
private string _validatingTime;
16+
private DateTime? _futureValidatingDate;
1517

1618
public PickersViewModel()
1719
{
@@ -39,6 +41,26 @@ public DateTime Time
3941
}
4042
}
4143

44+
public string ValidatingTime
45+
{
46+
get { return _validatingTime; }
47+
set
48+
{
49+
_validatingTime = value;
50+
OnPropertyChanged();
51+
}
52+
}
53+
54+
public DateTime? FutureValidatingDate
55+
{
56+
get { return _futureValidatingDate; }
57+
set
58+
{
59+
_futureValidatingDate = value;
60+
OnPropertyChanged();
61+
}
62+
}
63+
4264
public event PropertyChangedEventHandler PropertyChanged;
4365

4466
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)

MainDemo.Wpf/ProvingGround.xaml

Lines changed: 19 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -23,60 +23,25 @@
2323

2424

2525
<StackPanel>
26-
<WrapPanel Margin="24" Orientation="Horizontal">
27-
<materialDesign:Chip Content="James Willock" Margin="0 0 6 4">
28-
<materialDesign:Chip.Icon>
29-
<Image Source="Resources/ProfilePic.jpg" />
30-
</materialDesign:Chip.Icon>
31-
</materialDesign:Chip>
32-
<materialDesign:Chip Margin="0 0 4 4">
33-
Example Chip
34-
</materialDesign:Chip>
35-
<materialDesign:Chip Content="ANZ Bank"
36-
Icon="A"
37-
Margin="0 0 4 4" />
38-
<materialDesign:Chip Content="ZNA Inc"
39-
Icon="Z"
40-
Margin="0 0 4 4" />
41-
<materialDesign:Chip Content="Twitter"
42-
IconBackground="{DynamicResource PrimaryHueDarkBrush}"
43-
IconForeground="{DynamicResource PrimaryHueDarkForegroundBrush}"
44-
Margin="0 0 4 4">
45-
<materialDesign:Chip.Icon>
46-
<materialDesign:PackIcon Kind="Twitter"></materialDesign:PackIcon>
47-
</materialDesign:Chip.Icon>
48-
</materialDesign:Chip>
49-
</WrapPanel>
50-
<WrapPanel Margin="24" Orientation="Horizontal">
51-
<materialDesign:Chip Content="James Willock"
52-
IsDeletable="True"
53-
Margin="0 0 4 4"
54-
x:Name="ButtonsDemoChip"
55-
Click="ButtonsDemoChip_OnClick"
56-
DeleteClick="ButtonsDemoChip_OnDeleteClick"
57-
ToolTip="Just a tool tip"
58-
DeleteToolTip="Your friendly neighbour delete button"
59-
>
60-
<materialDesign:Chip.Icon>
61-
<Image Source="Resources/ProfilePic.jpg"></Image>
62-
</materialDesign:Chip.Icon>
63-
</materialDesign:Chip>
64-
<materialDesign:Chip Content="Example Chip"
65-
IsDeletable="True"
66-
ToolTip="This is an example chip"
67-
Margin="0 0 4 4">
68-
</materialDesign:Chip>
69-
<materialDesign:Chip Content="ANZ Bank"
70-
Icon="A"
71-
IsDeletable="True"
72-
Margin="0 0 4 4" />
73-
<materialDesign:Chip Content="ZNA Inc"
74-
Icon="Z"
75-
IsDeletable="True"
76-
IconBackground="{DynamicResource PrimaryHueLightBrush}"
77-
IconForeground="{DynamicResource PrimaryHueLightForegroundBrush}"
78-
Margin="0 0 4 4" />
79-
</WrapPanel>
26+
<ListBox>
27+
<Expander>
28+
<ListBox ItemsSource="{Binding Items}">
29+
30+
</ListBox>
31+
</Expander>
32+
<Expander>
33+
<ListBox ItemsSource="{Binding Items}">
34+
35+
</ListBox>
36+
</Expander>
37+
<Expander>
38+
<ListBox ItemsSource="{Binding Items}">
39+
40+
</ListBox>
41+
</Expander>
42+
</ListBox>
43+
44+
<Button Command="{Binding ClearItems}">Clear</Button>
8045
</StackPanel>
8146

8247
</UserControl>

MainDemo.Wpf/ProvingGround.xaml.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@ public class ProvingGroundViewModel : INotifyPropertyChanged
5151
{
5252
private string _name;
5353
private readonly ObservableCollection<SelectableViewModel> _items = CreateData();
54+
public ICommand ClearItems { get; }
5455

56+
public ProvingGroundViewModel()
57+
{
58+
ClearItems = new AnotherCommandImplementation(_ => Items.Clear());
59+
}
5560

5661
public string Name
5762
{

MainDemo.Wpf/TextFields.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
<TextBox.Text>
7373
<Binding Path="Name" UpdateSourceTrigger="PropertyChanged">
7474
<Binding.ValidationRules>
75-
<materialDesignDemo:NotEmptyValidationRule ValidatesOnTargetUpdated="True" />
75+
<domain1:NotEmptyValidationRule ValidatesOnTargetUpdated="True" />
7676
</Binding.ValidationRules>
7777
</Binding>
7878
</TextBox.Text>

0 commit comments

Comments
 (0)