Skip to content

Commit 8575360

Browse files
committed
allow popups to suppress validation #187
1 parent ba7d49f commit 8575360

10 files changed

+81
-19
lines changed

MainDemo.Wpf/Domain/DialogsViewModel.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.ComponentModel;
4-
using System.Linq;
53
using System.Runtime.CompilerServices;
6-
using System.Text;
74
using System.Threading.Tasks;
8-
using System.Windows.Controls;
95
using System.Windows.Input;
10-
using System.Windows.Threading;
6+
using MaterialDesignColors.WpfExample.Domain;
117
using MaterialDesignThemes.Wpf;
128

13-
namespace MaterialDesignColors.WpfExample.Domain
9+
namespace MaterialDesignDemo.Domain
1410
{
1511
public class DialogsViewModel : INotifyPropertyChanged
1612
{
@@ -33,7 +29,7 @@ private async void ExecuteRunDialog(object o)
3329
//let's set up a little MVVM, cos that's what the cool kids are doing:
3430
var view = new SampleDialog
3531
{
36-
DataContext = new object()
32+
DataContext = new SampleDialogViewModel()
3733
};
3834

3935
//show the dialog
@@ -53,7 +49,7 @@ private async void ExecuteRunExtendedDialog(object o)
5349
//let's set up a little MVVM, cos that's what the cool kids are doing:
5450
var view = new SampleDialog
5551
{
56-
DataContext = new object()
52+
DataContext = new SampleDialogViewModel()
5753
};
5854

5955
//show the dialog

MainDemo.Wpf/Domain/SampleDialog.xaml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
xmlns:local="clr-namespace:MaterialDesignColors.WpfExample.Domain"
77
xmlns:wpf="clr-namespace:MaterialDesignThemes.Wpf;assembly=MaterialDesignThemes.Wpf"
88
xmlns:system="clr-namespace:System;assembly=mscorlib"
9+
xmlns:wpfExample="clr-namespace:MaterialDesignColors.WpfExample"
10+
xmlns:materialDesignDemo="clr-namespace:MaterialDesignDemo"
911
mc:Ignorable="d"
1012
d:DesignHeight="300" d:DesignWidth="300">
1113
<Grid Margin="16">
@@ -17,10 +19,17 @@
1719
<RowDefinition />
1820
</Grid.RowDefinitions>
1921
<TextBlock>New contact:</TextBlock>
20-
<TextBox wpf:TextFieldAssist.Hint="Name" Style="{DynamicResource MaterialDesignFloatingHintTextBox}"
22+
<TextBox wpf:TextFieldAssist.Hint="Name" Style="{DynamicResource MaterialDesignFloatingHintTextBox}"
2123
Margin="0 6 0 0"
22-
FontSize="18" Grid.Row="1"
23-
/>
24+
FontSize="18" Grid.Row="1">
25+
<TextBox.Text>
26+
<Binding Path="Name" UpdateSourceTrigger="PropertyChanged">
27+
<Binding.ValidationRules>
28+
<materialDesignDemo:NotEmptyValidationRule ValidatesOnTargetUpdated="True" />
29+
</Binding.ValidationRules>
30+
</Binding>
31+
</TextBox.Text>
32+
</TextBox>
2433
<TextBox wpf:TextFieldAssist.Hint="Number" Style="{DynamicResource MaterialDesignFloatingHintTextBox}"
2534
Margin="0 8 0 0"
2635
FontSize="16" Grid.Row="2"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.ComponentModel;
3+
using MaterialDesignColors.WpfExample.Domain;
4+
5+
namespace MaterialDesignDemo.Domain
6+
{
7+
public class SampleDialogViewModel : INotifyPropertyChanged
8+
{
9+
private string _name;
10+
11+
public string Name
12+
{
13+
get { return _name; }
14+
set
15+
{
16+
this.MutateVerbose(ref _name, value, RaisePropertyChanged());
17+
}
18+
}
19+
20+
public event PropertyChangedEventHandler PropertyChanged;
21+
22+
private Action<PropertyChangedEventArgs> RaisePropertyChanged()
23+
{
24+
return args => PropertyChanged?.Invoke(this, args);
25+
}
26+
}
27+
}

MainDemo.Wpf/MainWindow.xaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
xmlns:domain="clr-namespace:MaterialDesignColors.WpfExample.Domain"
66
xmlns:system="clr-namespace:System;assembly=mscorlib"
77
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
8+
xmlns:domain1="clr-namespace:MaterialDesignDemo.Domain"
89
Title="Material Design in XAML" Height="800" Width="1100"
910
TextElement.Foreground="{DynamicResource MaterialDesignBody}"
1011
TextElement.FontWeight="Regular"
@@ -146,7 +147,7 @@
146147
<domain:DemoItem.Content>
147148
<wpfExample:Dialogs>
148149
<wpfExample:Dialogs.DataContext>
149-
<domain:DialogsViewModel />
150+
<domain1:DialogsViewModel />
150151
</wpfExample:Dialogs.DataContext>
151152
</wpfExample:Dialogs>
152153
</domain:DemoItem.Content>

MainDemo.Wpf/MaterialDesignDemo.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
<Compile Include="Domain\SampleDialog.xaml.cs">
9191
<DependentUpon>SampleDialog.xaml</DependentUpon>
9292
</Compile>
93+
<Compile Include="Domain\SampleDialogViewModel.cs" />
9394
<Compile Include="Domain\SampleMessageDialog.xaml.cs">
9495
<DependentUpon>SampleMessageDialog.xaml</DependentUpon>
9596
</Compile>

MainDemo.Wpf/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 MaterialDesignColors.WpfExample
4+
namespace MaterialDesignDemo
55
{
66
public class NotEmptyValidationRule : ValidationRule
77
{

MainDemo.Wpf/TextFields.xaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
xmlns:wpfExample="clr-namespace:MaterialDesignColors.WpfExample"
77
xmlns:domain="clr-namespace:MaterialDesignColors.WpfExample.Domain"
88
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
9+
xmlns:materialDesignDemo="clr-namespace:MaterialDesignDemo"
910
mc:Ignorable="d"
1011
d:DesignHeight="300" d:DesignWidth="600" Loaded="UserControl_Loaded"
1112
d:DataContext="{d:DesignInstance domain:TextFieldsViewModel, d:IsDesignTimeCreatable=False}">
@@ -64,7 +65,7 @@
6465
<TextBox.Text>
6566
<Binding Path="Name" UpdateSourceTrigger="PropertyChanged">
6667
<Binding.ValidationRules>
67-
<wpfExample:NotEmptyValidationRule ValidatesOnTargetUpdated="True" />
68+
<materialDesignDemo:NotEmptyValidationRule ValidatesOnTargetUpdated="True" />
6869
</Binding.ValidationRules>
6970
</Binding>
7071
</TextBox.Text>

MaterialDesignThemes.Wpf/DialogHost.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,9 @@ public object Identifier
224224

225225
private static void IsOpenPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
226226
{
227-
var dialogHost = (DialogHost)dependencyObject;
227+
var dialogHost = (DialogHost)dependencyObject;
228228

229+
ValidationAssist.SetSuppress(dialogHost._popupContentControl, !dialogHost.IsOpen);
229230
VisualStateManager.GoToState(dialogHost, dialogHost.SelectState(), !TransitionAssist.GetDisableTransitions(dialogHost));
230231

231232
if (!dialogHost.IsOpen)
@@ -441,7 +442,7 @@ internal void Close(object parameter)
441442
{
442443
var dialogClosingEventArgs = new DialogClosingEventArgs(_session, parameter, DialogClosingEvent);
443444

444-
_session.IsEnded = true;
445+
_session.IsEnded = true;
445446

446447
//multiple ways of calling back that the dialog is closing:
447448
// * routed event
@@ -456,7 +457,7 @@ internal void Close(object parameter)
456457
if (!dialogClosingEventArgs.IsCancelled)
457458
SetCurrentValue(IsOpenProperty, false);
458459
else
459-
_session.IsEnded = false;
460+
_session.IsEnded = false;
460461

461462
_closeDialogExecutionParameter = parameter;
462463
}
@@ -497,8 +498,8 @@ private void OpenDialogHandler(object sender, ExecutedRoutedEventArgs executedRo
497498

498499
DialogContent = executedRoutedEventArgs.Parameter;
499500
}
500-
501-
SetCurrentValue(IsOpenProperty, true);
501+
502+
ValidationAssist.SetSuppress(_popupContentControl, false);
502503

503504
executedRoutedEventArgs.Handled = true;
504505
}

MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.ValidationErrorTemplate.xaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<MultiDataTrigger.Conditions>
4040
<Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.OnlyShowOnFocus)}" Value="False"/>
4141
<Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.UsePopup)}" Value="True"/>
42+
<Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.Suppress)}" Value="False"/>
4243
</MultiDataTrigger.Conditions>
4344
<MultiDataTrigger.Setters>
4445
<Setter TargetName="ValidationPopup" Property="IsOpen" Value="True"/>
@@ -49,6 +50,7 @@
4950
<MultiDataTrigger.Conditions>
5051
<Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.OnlyShowOnFocus)}" Value="False"/>
5152
<Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.UsePopup)}" Value="False"/>
53+
<Condition Binding="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.Suppress)}" Value="False"/>
5254
</MultiDataTrigger.Conditions>
5355
<MultiDataTrigger.Setters>
5456
<Setter TargetName="DefaultErrorViewer" Property="Visibility" Value="Visible"/>

MaterialDesignThemes.Wpf/ValidationAssist.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,29 @@ public static void SetUsePopup(DependencyObject element, bool value)
5454
}
5555

5656
#endregion
57+
58+
/// <summary>
59+
/// Framework use only.
60+
/// </summary>
61+
public static readonly DependencyProperty SuppressProperty = DependencyProperty.RegisterAttached(
62+
"Suppress", typeof (bool), typeof (ValidationAssist), new FrameworkPropertyMetadata(default(bool), FrameworkPropertyMetadataOptions.Inherits));
63+
64+
/// <summary>
65+
/// Framework use only.
66+
/// </summary>
67+
/// <param name="element"></param>
68+
/// <param name="value"></param>
69+
public static void SetSuppress(DependencyObject element, bool value)
70+
{
71+
element.SetValue(SuppressProperty, value);
72+
}
73+
74+
/// <summary>
75+
/// Framework use only.
76+
/// </summary>
77+
public static bool GetSuppress(DependencyObject element)
78+
{
79+
return (bool) element.GetValue(SuppressProperty);
80+
}
5781
}
5882
}

0 commit comments

Comments
 (0)