Skip to content

Commit 909ba78

Browse files
NZSmartieKeboo
andauthored
Support use of ColorZoneAssist in ComboBox (#942)
* Support use of ColorZoneAssist in ComboBox First part is addressing #679. Only when the combobox displays it's popover will the colorzone take effect. This does not change the border away from the primary mid brush. * Add new attached TextFieldAssist.UnderlineBrush property * Update ComboBox examples appropiately * Fixed issue of transparent popup backgrounds. Co-authored-by: Kevin B <[email protected]>
1 parent 4c420ff commit 909ba78

File tree

8 files changed

+172
-33
lines changed

8 files changed

+172
-33
lines changed

MainDemo.Wpf/ColorZones.xaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@
5252
<StackPanel Orientation="Horizontal"
5353
materialDesign:RippleAssist.IsCentered="True">
5454
<ToggleButton Style="{DynamicResource MaterialDesignHamburgerToggleButton}" />
55+
<ComboBox SelectedIndex="0" Margin="8 0 0 0" BorderThickness="0"
56+
materialDesign:ColorZoneAssist.Mode="Standard"
57+
materialDesign:TextFieldAssist.UnderlineBrush="{DynamicResource MaterialDesignPaper}"
58+
BorderBrush="{DynamicResource MaterialDesignPaper}">
59+
<ComboBoxItem>Android</ComboBoxItem>
60+
<ComboBoxItem>iOS</ComboBoxItem>
61+
<ComboBoxItem>Linux</ComboBoxItem>
62+
<ComboBoxItem>Windows</ComboBoxItem>
63+
</ComboBox>
5564
<materialDesign:ColorZone Mode="Standard" Padding="8 4 8 4" CornerRadius="2" Panel.ZIndex="1"
5665
Margin="16 0 0 0"
5766
materialDesign:ShadowAssist.ShadowDepth="Depth1">

MainDemo.Wpf/TextFields.xaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@
169169
materialDesign:TextFieldAssist.HasClearButton="True"
170170
Style="{StaticResource MaterialDesignFloatingHintComboBox}"
171171
materialDesign:TextFieldAssist.SuffixText="sw"
172+
materialDesign:ColorZoneAssist.Mode="Inverted"
173+
materialDesign:TextFieldAssist.UnderlineBrush="{DynamicResource SecondaryAccentBrush}"
172174
materialDesign:HintAssist.HelperText="Select one OS">
173175
<ComboBoxItem>Android</ComboBoxItem>
174176
<ComboBoxItem>iOS</ComboBoxItem>
@@ -290,7 +292,7 @@
290292
</ComboBox>
291293
</smtx:XamlDisplay>
292294

293-
<materialDesign:PackIcon Grid.Row="6" Grid.Column="0" Kind="Key"
295+
<materialDesign:PackIcon Grid.Row="7" Grid.Column="0" Kind="Key"
294296
Margin="0 12 0 0"
295297
Foreground="{Binding ElementName=FloatingPasswordBox, Path=BorderBrush}" />
296298
<smtx:XamlDisplay Key="fields_18" Grid.Row="6" Grid.Column="1" Margin="0 12 0 0">

MainDemo.Wpf/Toggles.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@
267267
</StackPanel>
268268
<Border Margin="0 24 0 0" BorderThickness="0 1 0 0" BorderBrush="{DynamicResource MaterialDesignDivider}" Grid.Row="6" Grid.ColumnSpan="2" />
269269
<TextBlock Grid.Row="7" Style="{StaticResource MaterialDesignHeadline5TextBlock}" Margin="0 24">Checkboxes</TextBlock>
270-
<smtx:XamlDisplay Key="fields_31" Grid.Row="8" HorizontalAlignment="Left">
270+
<smtx:XamlDisplay Key="fields_35" Grid.Row="8" HorizontalAlignment="Left">
271271
<StackPanel Margin="8 0">
272272
<CheckBox IsChecked="True">Checked</CheckBox>
273273
<CheckBox IsChecked="False">Unchecked</CheckBox>

MaterialDesignThemes.Wpf/ComboBoxPopup.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ public bool ClassicMode
173173
public ComboBoxPopup()
174174
{
175175
CustomPopupPlacementCallback = ComboBoxCustomPopupPlacementCallback;
176+
176177
}
177178

178179
protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Windows.Data;
4+
using System.Windows.Media;
5+
6+
namespace MaterialDesignThemes.Wpf.Converters
7+
{
8+
internal class RemoveAlphaBrushConverter : IValueConverter
9+
{
10+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
11+
{
12+
if (value is SolidColorBrush brush)
13+
{
14+
Color background = Colors.White;
15+
if (parameter is Color color)
16+
{
17+
background = color;
18+
}
19+
return new SolidColorBrush(RgbaToRgb(brush.Color, background));
20+
}
21+
return Binding.DoNothing;
22+
}
23+
24+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
25+
{
26+
throw new NotImplementedException();
27+
}
28+
29+
private static Color RgbaToRgb(Color rgba, Color background)
30+
{
31+
double alpha = (double)rgba.A / byte.MaxValue;
32+
return Color.FromRgb(
33+
(byte)((1 - alpha) * background.R + alpha * rgba.R),
34+
(byte)((1 - alpha) * background.G + alpha * rgba.G),
35+
(byte)((1 - alpha) * background.B + alpha * rgba.B)
36+
);
37+
}
38+
}
39+
}

MaterialDesignThemes.Wpf/TextFieldAssist.cs

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,32 @@ public static Visibility GetDecorationVisibility(DependencyObject element)
6969
}
7070

7171
/// <summary>
72-
/// Controls the visibility of the filled text field.
72+
/// The attached WPF property for getting or setting the <see cref="Brush"/> value for an underline decoration.
73+
/// </summary>
74+
public static readonly DependencyProperty UnderlineBrushProperty = DependencyProperty.RegisterAttached(
75+
"UnderlineBrush", typeof(Brush), typeof(TextFieldAssist), new PropertyMetadata(Brushes.Transparent));
76+
77+
/// <summary>
78+
/// Sets the <see cref="Brush"/> used for underline decoration.
79+
/// </summary>
80+
/// <param name="element"></param>
81+
/// <param name="value"></param>
82+
public static void SetUnderlineBrush(DependencyObject element, Brush value)
83+
{
84+
element.SetValue(UnderlineBrushProperty, value);
85+
}
86+
87+
/// <summary>
88+
/// Gets the <see cref="Brush"/> used for underline decoration.
89+
/// </summary>
90+
/// <param name="element"></param>
91+
public static Brush GetUnderlineBrush(DependencyObject element)
92+
{
93+
return (Brush)element.GetValue(UnderlineBrushProperty);
94+
}
95+
96+
/// <summary>
97+
/// Controls the visbility of the text field box.
7398
/// </summary>
7499
public static readonly DependencyProperty HasFilledTextFieldProperty = DependencyProperty.RegisterAttached(
75100
"HasFilledTextField", typeof(bool), typeof(TextFieldAssist), new PropertyMetadata(false));
@@ -164,28 +189,6 @@ public static bool GetRippleOnFocusEnabled(DependencyObject element)
164189
return (bool)element.GetValue(RippleOnFocusEnabledProperty);
165190
}
166191

167-
/// <summary>
168-
/// The color for highlighting effects on the border of a text box.
169-
/// </summary>
170-
public static readonly DependencyProperty UnderlineBrushProperty = DependencyProperty.RegisterAttached(
171-
"UnderlineBrush", typeof(Brush), typeof(TextFieldAssist), new PropertyMetadata(null));
172-
173-
/// <summary>
174-
/// Sets the color for highlighting effects on the border of a text box.
175-
/// </summary>
176-
public static void SetUnderlineBrush(DependencyObject element, Brush value)
177-
{
178-
element.SetValue(UnderlineBrushProperty, value);
179-
}
180-
181-
/// <summary>
182-
/// Gets the color for highlighting effects on the border of a text box.
183-
/// </summary>
184-
public static Brush GetUnderlineBrush(DependencyObject element)
185-
{
186-
return (Brush)element.GetValue(UnderlineBrushProperty);
187-
}
188-
189192
/// <summary>
190193
/// Automatically inserts spelling suggestions into the text box context menu.
191194
/// </summary>

MaterialDesignThemes.Wpf/Themes/Generic.xaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@
191191
</Style>
192192

193193
<Style TargetType="{x:Type local:Underline}">
194-
<Setter Property="Background" Value="{DynamicResource PrimaryHueMidBrush}"/>
194+
<Setter Property="local:TextFieldAssist.UnderlineBrush" Value="{DynamicResource PrimaryHueMidBrush}"/>
195+
<Setter Property="Background" Value="{Binding Path=(local:TextFieldAssist.UnderlineBrush), RelativeSource={RelativeSource Self}}"/>
195196
<Setter Property="SnapsToDevicePixels" Value="True"/>
196197
<Setter Property="HorizontalAlignment" Value="Stretch"/>
197198
<Setter Property="VerticalAlignment" Value="Bottom"/>

MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.ComboBox.xaml

Lines changed: 91 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
33
xmlns:converters="clr-namespace:MaterialDesignThemes.Wpf.Converters"
44
xmlns:system="clr-namespace:System;assembly=mscorlib"
5-
xmlns:wpf="clr-namespace:MaterialDesignThemes.Wpf"
6-
xmlns:materialDesign="clr-namespace:MaterialDesignThemes.Wpf">
5+
xmlns:wpf="clr-namespace:MaterialDesignThemes.Wpf">
76

87
<ResourceDictionary.MergedDictionaries>
98
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Shadows.xaml" />
@@ -18,6 +17,7 @@
1817
<converters:TextFieldClearButtonVisibilityConverter x:Key="ClearTextConverter" />
1918
<converters:NotConverter x:Key="NotConverter" />
2019
<converters:FallbackBrushConverter x:Key="FallbackBrushConverter" />
20+
<converters:RemoveAlphaBrushConverter x:Key="RemoveAlphaBrushConverter" />
2121

2222
<system:Double x:Key="PopupContentPresenterExtend">4</system:Double>
2323
<system:Double x:Key="PopupTopBottomMargin">8</system:Double>
@@ -494,7 +494,8 @@
494494
X1="0" X2="{Binding ActualWidth, ElementName=toggleButton}" Y1="0" Y2="0"
495495
Stroke="{TemplateBinding BorderBrush}" Opacity="0.56" />
496496
<wpf:Underline x:Name="Underline"
497-
Grid.ColumnSpan="2" Grid.Column="0"
497+
Grid.ColumnSpan="2"
498+
wpf:TextFieldAssist.UnderlineBrush="{Binding Path=(wpf:TextFieldAssist.UnderlineBrush), RelativeSource={RelativeSource TemplatedParent}}"
498499
IsActive="{Binding ElementName=PART_EditableTextBox, Path=IsKeyboardFocused}"
499500
Visibility="{Binding Path=(wpf:TextFieldAssist.DecorationVisibility), RelativeSource={RelativeSource TemplatedParent}}"
500501
Background="{Binding Path=(wpf:TextFieldAssist.UnderlineBrush), RelativeSource={RelativeSource TemplatedParent}}" />
@@ -519,6 +520,7 @@
519520
DefaultVerticalOffset="5"
520521
DownVerticalOffset="-15.5"
521522
UpVerticalOffset="15"
523+
wpf:ColorZoneAssist.Mode="{Binding Path=(wpf:ColorZoneAssist.Mode), RelativeSource={RelativeSource TemplatedParent}}"
522524
Tag="{DynamicResource MaterialDesignPaper}"
523525
ClassicMode="{Binding Path=(wpf:ComboBoxAssist.ClassicMode), RelativeSource={RelativeSource TemplatedParent}}"
524526
UpContentTemplate="{StaticResource PopupContentUpTemplate}"
@@ -541,12 +543,12 @@
541543
</Grid>
542544
</Grid>
543545
<ControlTemplate.Triggers>
544-
<Trigger SourceName="PART_Popup" Property="PopupPlacement" Value="{x:Static materialDesign:ComboBoxPopupPlacement.Classic}">
546+
<Trigger SourceName="PART_Popup" Property="PopupPlacement" Value="{x:Static wpf:ComboBoxPopupPlacement.Classic}">
545547
<Setter Property="ItemContainerStyle" Value="{StaticResource MaterialDesignComboBoxItemStyle}" />
546548
</Trigger>
547549
<Trigger SourceName="PART_Popup" Property="IsOpen" Value="True">
548550
<Setter Property="Background" TargetName="templateRoot" Value="{Binding Background, ElementName=PART_Popup}" />
549-
</Trigger>
551+
</Trigger>
550552
<Trigger Property="IsEnabled" Value="False">
551553
<Setter TargetName="templateRoot" Property="Opacity" Value="0.56"/>
552554
<Setter TargetName="toggleButton" Property="BorderBrush" Value="Transparent"/>
@@ -628,8 +630,88 @@
628630
<Condition Property="IsMouseOver" Value="true" />
629631
<Condition Property="Validation.HasError" Value="false" />
630632
</MultiTrigger.Conditions>
631-
<Setter Property="BorderBrush" Value="{Binding Path=(wpf:TextFieldAssist.UnderlineBrush), RelativeSource={RelativeSource Self}}" />
632-
<Setter TargetName="Underline" Property="Background" Value="{Binding Path=(wpf:TextFieldAssist.UnderlineBrush), RelativeSource={RelativeSource TemplatedParent}}" />
633+
<Setter Property="BorderBrush" Value="{Binding Path=(wpf:TextFieldAssist.UnderlineBrush), RelativeSource={RelativeSource Self}}"/>
634+
<Setter TargetName="Underline" Property="Background" Value="{Binding Path=(wpf:TextFieldAssist.UnderlineBrush), RelativeSource={RelativeSource Self}}"/>
635+
</MultiTrigger>
636+
<MultiTrigger>
637+
<MultiTrigger.Conditions>
638+
<Condition SourceName="PART_Popup" Property="IsOpen" Value="True"/>
639+
<Condition SourceName="PART_Popup" Property="wpf:ColorZoneAssist.Mode" Value="Standard"/>
640+
</MultiTrigger.Conditions>
641+
<Setter TargetName="PART_Popup" Property="Tag" Value="{DynamicResource MaterialDesignPaper}" />
642+
<Setter TargetName="PART_Popup" Property="Background"
643+
Value="{Binding Tag, RelativeSource={RelativeSource Self}, Converter={StaticResource RemoveAlphaBrushConverter}}" />
644+
<Setter Property="TextElement.Foreground" Value="{DynamicResource MaterialDesignBody}" />
645+
</MultiTrigger>
646+
<MultiTrigger>
647+
<MultiTrigger.Conditions>
648+
<Condition SourceName="PART_Popup" Property="IsOpen" Value="True"/>
649+
<Condition SourceName="PART_Popup" Property="wpf:ColorZoneAssist.Mode" Value="Inverted"/>
650+
</MultiTrigger.Conditions>
651+
<Setter TargetName="PART_Popup" Property="Tag" Value="{DynamicResource MaterialDesignBody}" />
652+
<Setter TargetName="PART_Popup" Property="Background"
653+
Value="{Binding Tag, RelativeSource={RelativeSource Self}, Converter={StaticResource RemoveAlphaBrushConverter}}" />
654+
<Setter Property="TextElement.Foreground" Value="{DynamicResource MaterialDesignPaper}" />
655+
</MultiTrigger>
656+
<MultiTrigger>
657+
<MultiTrigger.Conditions>
658+
<Condition SourceName="PART_Popup" Property="IsOpen" Value="True"/>
659+
<Condition SourceName="PART_Popup" Property="wpf:ColorZoneAssist.Mode" Value="PrimaryLight" />
660+
</MultiTrigger.Conditions>
661+
<Setter TargetName="PART_Popup" Property="Tag" Value="{DynamicResource PrimaryHueLightBrush}" />
662+
<Setter TargetName="PART_Popup" Property="Background"
663+
Value="{Binding Tag, RelativeSource={RelativeSource Self}, Converter={StaticResource RemoveAlphaBrushConverter}}" />
664+
<Setter Property="TextElement.Foreground" Value="{DynamicResource PrimaryHueLightForegroundBrush}" />
665+
</MultiTrigger>
666+
<MultiTrigger>
667+
<MultiTrigger.Conditions>
668+
<Condition SourceName="PART_Popup" Property="IsOpen" Value="True"/>
669+
<Condition SourceName="PART_Popup" Property="wpf:ColorZoneAssist.Mode" Value="PrimaryMid"/>
670+
</MultiTrigger.Conditions>
671+
<Setter TargetName="PART_Popup" Property="Tag" Value="{DynamicResource PrimaryHueMidBrush}" />
672+
<Setter TargetName="PART_Popup" Property="Background"
673+
Value="{Binding Tag, RelativeSource={RelativeSource Self}, Converter={StaticResource RemoveAlphaBrushConverter}}" />
674+
<Setter Property="TextElement.Foreground" Value="{DynamicResource PrimaryHueMidForegroundBrush}" />
675+
</MultiTrigger>
676+
<MultiTrigger>
677+
<MultiTrigger.Conditions>
678+
<Condition SourceName="PART_Popup" Property="IsOpen" Value="True"/>
679+
<Condition SourceName="PART_Popup" Property="wpf:ColorZoneAssist.Mode" Value="PrimaryDark"/>
680+
</MultiTrigger.Conditions>
681+
<Setter TargetName="PART_Popup" Property="Tag" Value="{DynamicResource PrimaryHueDarkBrush}" />
682+
<Setter TargetName="PART_Popup" Property="Background"
683+
Value="{Binding Tag, RelativeSource={RelativeSource Self}, Converter={StaticResource RemoveAlphaBrushConverter}}" />
684+
<Setter Property="TextElement.Foreground" Value="{DynamicResource PrimaryHueDarkForegroundBrush}" />
685+
</MultiTrigger>
686+
<MultiTrigger>
687+
<MultiTrigger.Conditions>
688+
<Condition SourceName="PART_Popup" Property="IsOpen" Value="True"/>
689+
<Condition SourceName="PART_Popup" Property="wpf:ColorZoneAssist.Mode" Value="Accent"/>
690+
</MultiTrigger.Conditions>
691+
<Setter TargetName="PART_Popup" Property="Tag" Value="{DynamicResource SecondaryAccentBrush}" />
692+
<Setter TargetName="PART_Popup" Property="Background"
693+
Value="{Binding Tag, RelativeSource={RelativeSource Self}, Converter={StaticResource RemoveAlphaBrushConverter}}" />
694+
<Setter Property="TextElement.Foreground" Value="{DynamicResource SecondaryAccentForegroundBrush}" />
695+
</MultiTrigger>
696+
<MultiTrigger>
697+
<MultiTrigger.Conditions>
698+
<Condition SourceName="PART_Popup" Property="IsOpen" Value="True"/>
699+
<Condition SourceName="PART_Popup" Property="wpf:ColorZoneAssist.Mode" Value="Light"/>
700+
</MultiTrigger.Conditions>
701+
<Setter TargetName="PART_Popup" Property="Tag" Value="{DynamicResource MaterialDesignLightBackground}" />
702+
<Setter TargetName="PART_Popup" Property="Background"
703+
Value="{Binding Tag, RelativeSource={RelativeSource Self}, Converter={StaticResource RemoveAlphaBrushConverter}}" />
704+
<Setter Property="TextElement.Foreground" Value="{DynamicResource MaterialDesignLightForeground}" />
705+
</MultiTrigger>
706+
<MultiTrigger>
707+
<MultiTrigger.Conditions>
708+
<Condition SourceName="PART_Popup" Property="IsOpen" Value="True"/>
709+
<Condition SourceName="PART_Popup" Property="wpf:ColorZoneAssist.Mode" Value="Dark"/>
710+
</MultiTrigger.Conditions>
711+
<Setter TargetName="PART_Popup" Property="Tag" Value="{DynamicResource MaterialDesignDarkBackground}" />
712+
<Setter TargetName="PART_Popup" Property="Background"
713+
Value="{Binding Tag, RelativeSource={RelativeSource Self}, Converter={StaticResource RemoveAlphaBrushConverter}}" />
714+
<Setter Property="TextElement.Foreground" Value="{DynamicResource MaterialDesignDarkForeground}" />
633715
</MultiTrigger>
634716
</ControlTemplate.Triggers>
635717
</ControlTemplate>
@@ -654,6 +736,8 @@
654736
<Setter Property="Validation.ErrorTemplate" Value="{StaticResource MaterialDesignValidationErrorTemplate}"/>
655737
<Setter Property="wpf:TextFieldAssist.TextBoxViewMargin" Value="1 0 1 0" />
656738
<Setter Property="wpf:TextFieldAssist.UnderlineBrush" Value="{DynamicResource PrimaryHueMidBrush}" />
739+
<Setter Property="wpf:ColorZoneAssist.Mode" Value="Standard" />
740+
<Setter Property="wpf:TextFieldAssist.UnderlineBrush" Value="{DynamicResource PrimaryHueMidBrush}" />
657741
<Setter Property="wpf:HintAssist.Foreground" Value="{DynamicResource PrimaryHueMidBrush}" />
658742
<Setter Property="Template" Value="{StaticResource MaterialDesignFloatingHintComboBoxTemplate}" />
659743
<Style.Triggers>

0 commit comments

Comments
 (0)