Skip to content

Commit 40ee2e5

Browse files
TextField LeadingIcon (#2221)
* WIP: TextField Leading Icon * LeadingIconSize added, TextBox template changed for icon
1 parent 4765ca3 commit 40ee2e5

File tree

2 files changed

+92
-43
lines changed

2 files changed

+92
-43
lines changed

MaterialDesignThemes.Wpf/TextFieldAssist.cs

Lines changed: 76 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,80 @@ public static class TextFieldAssist
144144

145145
public static bool GetIncludeSpellingSuggestions(TextBoxBase element) => (bool)element.GetValue(IncludeSpellingSuggestionsProperty);
146146

147+
/// <summary>
148+
/// SuffixText dependency property
149+
/// </summary>
150+
public static readonly DependencyProperty SuffixTextProperty = DependencyProperty.RegisterAttached(
151+
"SuffixText", typeof(string), typeof(TextFieldAssist), new PropertyMetadata(default(string?)));
152+
153+
public static void SetSuffixText(DependencyObject element, string? value)
154+
=> element.SetValue(SuffixTextProperty, value);
155+
156+
public static string? GetSuffixText(DependencyObject element)
157+
=> (string?)element.GetValue(SuffixTextProperty);
158+
159+
/// <summary>
160+
/// PrefixText dependency property
161+
/// </summary>
162+
public static readonly DependencyProperty PrefixTextProperty = DependencyProperty.RegisterAttached(
163+
"PrefixText", typeof(string), typeof(TextFieldAssist), new PropertyMetadata(default(string?)));
164+
165+
public static void SetPrefixText(DependencyObject element, string? value)
166+
=> element.SetValue(PrefixTextProperty, value);
167+
168+
public static string? GetPrefixText(DependencyObject element)
169+
=> (string?)element.GetValue(PrefixTextProperty);
170+
171+
/// <summary>
172+
/// Controls the visbility of the clear button.
173+
/// </summary>
174+
public static readonly DependencyProperty HasClearButtonProperty = DependencyProperty.RegisterAttached(
175+
"HasClearButton", typeof(bool), typeof(TextFieldAssist), new PropertyMetadata(false, HasClearButtonChanged));
176+
177+
public static void SetHasClearButton(DependencyObject element, bool value)
178+
=> element.SetValue(HasClearButtonProperty, value);
179+
180+
public static bool GetHasClearButton(DependencyObject element)
181+
=> (bool)element.GetValue(HasClearButtonProperty);
182+
183+
/// <summary>
184+
/// Controls visibility of the leading icon
185+
/// </summary>
186+
public static readonly DependencyProperty HasLeadingIconProperty = DependencyProperty.RegisterAttached(
187+
"HasLeadingIcon", typeof(bool), typeof(TextFieldAssist), new PropertyMetadata(default(bool)));
188+
189+
public static void SetHasLeadingIcon(DependencyObject element, bool value)
190+
=> element.SetValue(HasLeadingIconProperty, value);
191+
192+
public static bool GetHasLeadingIcon(DependencyObject element)
193+
=> (bool)element.GetValue(HasLeadingIconProperty);
194+
195+
/// <summary>
196+
/// Controls the leading icon
197+
/// </summary>
198+
public static readonly DependencyProperty LeadingIconProperty = DependencyProperty.RegisterAttached(
199+
"LeadingIcon", typeof(PackIconKind), typeof(TextFieldAssist), new PropertyMetadata());
200+
201+
public static void SetLeadingIcon(DependencyObject element, PackIconKind value)
202+
=> element.SetValue(LeadingIconProperty, value);
203+
204+
public static PackIconKind GetLeadingIcon(DependencyObject element)
205+
=> (PackIconKind)element.GetValue(LeadingIconProperty);
206+
207+
/// <summary>
208+
/// Controls the size of the leading icon
209+
/// </summary>
210+
public static readonly DependencyProperty LeadingIconSizeProperty = DependencyProperty.RegisterAttached(
211+
"LeadingIconSize", typeof(double), typeof(TextFieldAssist), new PropertyMetadata(20.0));
212+
213+
public static void SetLeadingIconSize(DependencyObject element, double value)
214+
=> element.SetValue(LeadingIconSizeProperty, value);
215+
216+
public static double GetLeadingIconSize(DependencyObject element)
217+
=> (double)element.GetValue(LeadingIconSizeProperty);
218+
219+
#region Methods
220+
147221
private static void IncludeSpellingSuggestionsChanged(DependencyObject element, DependencyPropertyChangedEventArgs e)
148222
{
149223
if (element is TextBoxBase textBox)
@@ -255,13 +329,7 @@ where ReferenceEquals(item.Tag, typeof(Spelling))
255329
{
256330
menu.Items.Remove(item);
257331
}
258-
}
259-
260-
/// <summary>
261-
/// Controls the visbility of the clear button.
262-
/// </summary>
263-
public static readonly DependencyProperty HasClearButtonProperty = DependencyProperty.RegisterAttached(
264-
"HasClearButton", typeof(bool), typeof(TextFieldAssist), new PropertyMetadata(false, HasClearButtonChanged));
332+
}
265333

266334
private static void HasClearButtonChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
267335
{
@@ -299,39 +367,7 @@ private static void SetClearHandler(Control box)
299367
clearButton.Click -= handler;
300368
}
301369
}
302-
303-
public static void SetHasClearButton(DependencyObject element, bool value)
304-
=> element.SetValue(HasClearButtonProperty, value);
305-
306-
public static bool GetHasClearButton(DependencyObject element)
307-
=> (bool)element.GetValue(HasClearButtonProperty);
308-
309-
/// <summary>
310-
/// SuffixText dependency property
311-
/// </summary>
312-
public static readonly DependencyProperty SuffixTextProperty = DependencyProperty.RegisterAttached(
313-
"SuffixText", typeof(string), typeof(TextFieldAssist), new PropertyMetadata(default(string?)));
314-
315-
public static void SetSuffixText(DependencyObject element, string? value)
316-
=> element.SetValue(SuffixTextProperty, value);
317-
318-
public static string? GetSuffixText(DependencyObject element)
319-
=> (string?)element.GetValue(SuffixTextProperty);
320-
321-
/// <summary>
322-
/// PrefixText dependency property
323-
/// </summary>
324-
public static readonly DependencyProperty PrefixTextProperty = DependencyProperty.RegisterAttached(
325-
"PrefixText", typeof(string), typeof(TextFieldAssist), new PropertyMetadata(default(string?)));
326-
327-
public static void SetPrefixText(DependencyObject element, string? value)
328-
=> element.SetValue(PrefixTextProperty, value);
329-
330-
public static string? GetPrefixText(DependencyObject element)
331-
=> (string?)element.GetValue(PrefixTextProperty);
332-
333-
#region Methods
334-
370+
335371
/// <summary>
336372
/// Applies the text box view margin.
337373
/// </summary>

MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.TextBox.xaml

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,31 @@
9191
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
9292
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}">
9393
<Grid.ColumnDefinitions>
94+
<ColumnDefinition Width="Auto" />
9495
<ColumnDefinition />
9596
<ColumnDefinition Width="Auto" />
9697
</Grid.ColumnDefinitions>
98+
<wpf:PackIcon
99+
Grid.Column="0"
100+
x:Name="LeadingPackIcon"
101+
Margin="0 0 8 0"
102+
Height="{TemplateBinding wpf:TextFieldAssist.LeadingIconSize}"
103+
Width="{TemplateBinding wpf:TextFieldAssist.LeadingIconSize}"
104+
VerticalAlignment="Bottom"
105+
Opacity="{TemplateBinding wpf:HintAssist.HintOpacity}"
106+
Visibility="{TemplateBinding wpf:TextFieldAssist.HasLeadingIcon, Converter={StaticResource BooleanToVisibilityConverter}}"
107+
Kind="{TemplateBinding wpf:TextFieldAssist.LeadingIcon}" />
108+
97109
<Grid
98-
x:Name="grid"
99-
VerticalAlignment="Center"
110+
Grid.Column="1"
111+
x:Name="grid"
100112
MinWidth="1">
101113
<Grid.ColumnDefinitions>
102114
<ColumnDefinition Width="Auto" />
103115
<ColumnDefinition Width="*" />
104116
<ColumnDefinition Width="Auto" />
105117
</Grid.ColumnDefinitions>
118+
106119
<WrapPanel
107120
Grid.Column="0">
108121
<TextBlock
@@ -159,7 +172,7 @@
159172
Text="{TemplateBinding wpf:TextFieldAssist.SuffixText}" />
160173
</Grid>
161174
<Button
162-
Grid.Column="1"
175+
Grid.Column="2"
163176
x:Name="PART_ClearButton"
164177
Height="Auto"
165178
Padding="2 0 0 0"

0 commit comments

Comments
 (0)