Skip to content

Commit 4ef1a91

Browse files
committed
Set BorderBrush and Underline to ValidationErrorBrush in ComboBox
1 parent 6f43d13 commit 4ef1a91

File tree

5 files changed

+64
-8
lines changed

5 files changed

+64
-8
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Globalization;
2+
using System.Windows.Controls;
3+
4+
namespace MaterialDesignDemo.Domain
5+
{
6+
public class NotNullValidationRule : ValidationRule
7+
{
8+
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
9+
{
10+
return value == null ? new ValidationResult(false, "Field is required.") : ValidationResult.ValidResult;
11+
}
12+
}
13+
}

MainDemo.Wpf/Domain/TextFieldsViewModel.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ public class TextFieldsViewModel : INotifyPropertyChanged
1414
{
1515
private string _name;
1616
private int _selectedValueOne;
17+
private int? _selectedValueTwo;
1718

1819
public TextFieldsViewModel()
1920
{
2021
LongListToTestComboVirtualization = new List<int>(Enumerable.Range(0, 1000));
2122

2223
SelectedValueOne = LongListToTestComboVirtualization.Skip(2).First();
24+
SelectedValueTwo = null;
2325
}
2426

2527
public string Name
@@ -40,6 +42,15 @@ public int SelectedValueOne
4042
}
4143
}
4244

45+
public int? SelectedValueTwo
46+
{
47+
get { return _selectedValueTwo; }
48+
set
49+
{
50+
this.MutateVerbose(ref _selectedValueTwo, value, RaisePropertyChanged());
51+
}
52+
}
53+
4354
public IList<int> LongListToTestComboVirtualization { get; }
4455

4556
public DemoItem DemoItem => new DemoItem { Name = "Mr. Test"};

MainDemo.Wpf/MaterialDesignDemo.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
<Compile Include="Domain\DemoItem.cs" />
9494
<Compile Include="Domain\DialogsViewModel.cs" />
9595
<Compile Include="Domain\ListFieldsViewModel.cs" />
96+
<Compile Include="Domain\NotNullValidationRule.cs" />
9697
<Compile Include="Domain\Sample4Dialog.xaml.cs">
9798
<DependentUpon>Sample4Dialog.xaml</DependentUpon>
9899
</Compile>

MainDemo.Wpf/TextFields.xaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@
130130
VerticalAlignment="Bottom"
131131
ItemsSource="{Binding LongListToTestComboVirtualization}"
132132
SelectedValue="{Binding SelectedValueOne}">
133+
<ComboBox.SelectedItem>
134+
<Binding Path="SelectedValueOne" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
135+
<Binding.ValidationRules>
136+
<domain1:NotNullValidationRule ValidatesOnTargetUpdated="True"/>
137+
</Binding.ValidationRules>
138+
</Binding>
139+
</ComboBox.SelectedItem>
133140
<ComboBox.ItemsPanel>
134141
<ItemsPanelTemplate>
135142
<VirtualizingStackPanel />
@@ -142,6 +149,13 @@
142149
VerticalAlignment="Bottom"
143150
IsEditable="True"
144151
ItemsSource="{Binding LongListToTestComboVirtualization}">
152+
<ComboBox.SelectedItem>
153+
<Binding Path="SelectedValueTwo" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
154+
<Binding.ValidationRules>
155+
<domain1:NotNullValidationRule ValidatesOnTargetUpdated="True"/>
156+
</Binding.ValidationRules>
157+
</Binding>
158+
</ComboBox.SelectedItem>
145159
<ComboBox.ItemsPanel>
146160
<ItemsPanelTemplate>
147161
<VirtualizingStackPanel />

MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.ComboBox.xaml

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<converters:TextFieldHintVisibilityConverter x:Key="TextFieldHintVisibilityConverter" IsNotEmptyValue="Collapsed" />
1313
<converters:MathConverter x:Key="MathAddConverter" Operation="Add" />
1414
<converters:BrushRoundConverter x:Key="BrushRoundConverter" />
15-
15+
1616
<system:Double x:Key="PopupContentPresenterExtend">4</system:Double>
1717
<system:Double x:Key="PopupTopBottomMargin">8</system:Double>
1818
<system:Double x:Key="PopupLeftRightMargin">16</system:Double>
@@ -86,7 +86,7 @@
8686
</Grid>
8787
</Grid>
8888
</ControlTemplate>
89-
89+
9090
<ControlTemplate x:Key="PopupContentDownTemplate" TargetType="ContentControl">
9191
<Grid MinWidth="{Binding ElementName=templateRoot, Path=ActualWidth, Converter={StaticResource MathAddConverter}, ConverterParameter=32}"
9292
Margin="6">
@@ -114,7 +114,7 @@
114114
CornerRadius="2 2 0 0"
115115
Background="{Binding ElementName=PART_Popup, Path=Background}"
116116
Height="{StaticResource PopupTopBottomMargin}"/>
117-
117+
118118
<Grid Grid.Row="1">
119119
<Grid.ColumnDefinitions>
120120
<ColumnDefinition Width="Auto"/>
@@ -147,7 +147,7 @@
147147
</Grid>
148148
</Grid>
149149
</ControlTemplate>
150-
150+
151151
<ControlTemplate x:Key="PopupContentDefaultTemplate" TargetType="ContentControl">
152152
<Grid MinWidth="{Binding ElementName=templateRoot, Path=ActualWidth, Converter={StaticResource MathAddConverter}, ConverterParameter=32}"
153153
Margin="6">
@@ -424,7 +424,7 @@
424424
IsReadOnly="{Binding IsReadOnly, RelativeSource={RelativeSource TemplatedParent}}"
425425
Style="{StaticResource MaterialDesignComboBoxEditableTextBox}"
426426
Visibility="Collapsed" />
427-
427+
428428
<wpf:SmartHint x:Name="Hint"
429429
HintProxy="{Binding RelativeSource={RelativeSource TemplatedParent}, Converter={x:Static converters:HintProxyFabricConverter.Instance}}"
430430
FontSize="{TemplateBinding FontSize}"
@@ -520,6 +520,26 @@
520520
<Trigger Property="IsKeyboardFocused" Value="true">
521521
<Setter TargetName="Underline" Property="IsActive" Value="True"/>
522522
</Trigger>
523+
<Trigger Property="Validation.HasError" Value="true">
524+
<Setter Property="BorderBrush" Value="{DynamicResource ValidationErrorBrush}"/>
525+
<Setter TargetName="Underline" Property="Background" Value="{DynamicResource ValidationErrorBrush}"/>
526+
</Trigger>
527+
<MultiTrigger>
528+
<MultiTrigger.Conditions>
529+
<Condition Property="IsMouseOver" Value="true" />
530+
<Condition Property="Validation.HasError" Value="true" />
531+
</MultiTrigger.Conditions>
532+
<Setter Property="BorderBrush" Value="{DynamicResource ValidationErrorBrush}"/>
533+
<Setter TargetName="Underline" Property="Background" Value="{DynamicResource ValidationErrorBrush}"/>
534+
</MultiTrigger>
535+
<MultiTrigger>
536+
<MultiTrigger.Conditions>
537+
<Condition Property="IsMouseOver" Value="true" />
538+
<Condition Property="Validation.HasError" Value="false" />
539+
</MultiTrigger.Conditions>
540+
<Setter Property="BorderBrush" Value="{DynamicResource PrimaryHueMidBrush}"/>
541+
<Setter TargetName="Underline" Property="Background" Value="{DynamicResource PrimaryHueMidBrush}"/>
542+
</MultiTrigger>
523543
</ControlTemplate.Triggers>
524544
</ControlTemplate>
525545

@@ -543,9 +563,6 @@
543563
<Setter Property="Validation.ErrorTemplate" Value="{StaticResource MaterialDesignValidationErrorTemplate}"/>
544564
<Setter Property="Template" Value="{StaticResource MaterialDesignFloatingHintComboBoxTemplate}" />
545565
<Style.Triggers>
546-
<Trigger Property="IsMouseOver" Value="true">
547-
<Setter Property="BorderBrush" Value="{DynamicResource PrimaryHueMidBrush}" />
548-
</Trigger>
549566
<Trigger Property="IsKeyboardFocused" Value="true">
550567
<Setter Property="BorderBrush" Value="{DynamicResource PrimaryHueMidBrush}" />
551568
</Trigger>

0 commit comments

Comments
 (0)