Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.ComponentModel;

namespace MaterialDesignThemes.UITests.WPF.ContentControls;

public class ContentControlTests : TestBase
{
public ContentControlTests(ITestOutputHelper output)
: base(output)
{
}


[Fact]
[Description("Issue 2510")]
public async Task ClearButton_InsideOfControlTemplate_CanStillClearContent()
{
await using var recorder = new TestRecorder(App);

//Arrange
var grid = await LoadXaml<ContentControl>(@"
<ContentControl>
<ContentControl.ContentTemplate>
<DataTemplate>
<TextBox Text=""Some Text"" materialDesign:TextFieldAssist.HasClearButton=""True""/>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>");
var textBox = await grid.GetElement<TextBox>("/TextBox");
var clearButton = await grid.GetElement<Button>("PART_ClearButton");

string? initial = await textBox.GetText();

//Act
await clearButton.LeftClick();

//Assert
Assert.Equal("Some Text", initial);
Assert.True(string.IsNullOrEmpty(await textBox.GetText()));

recorder.Success();
}

}
81 changes: 40 additions & 41 deletions MaterialDesignThemes.Wpf/Internal/ClearText.cs
Original file line number Diff line number Diff line change
@@ -1,59 +1,58 @@
namespace MaterialDesignThemes.Wpf.Internal
namespace MaterialDesignThemes.Wpf.Internal;

public static class ClearText
{
public static class ClearText
{
public static readonly RoutedCommand ClearCommand = new();
public static readonly RoutedCommand ClearCommand = new();

public static bool GetHandlesClearCommand(DependencyObject obj)
=> (bool)obj.GetValue(HandlesClearCommandProperty);
public static bool GetHandlesClearCommand(DependencyObject obj)
=> (bool)obj.GetValue(HandlesClearCommandProperty);

public static void SetHandlesClearCommand(DependencyObject obj, bool value)
=> obj.SetValue(HandlesClearCommandProperty, value);
public static void SetHandlesClearCommand(DependencyObject obj, bool value)
=> obj.SetValue(HandlesClearCommandProperty, value);

public static readonly DependencyProperty HandlesClearCommandProperty =
DependencyProperty.RegisterAttached("HandlesClearCommand", typeof(bool), typeof(ClearText), new PropertyMetadata(false, OnHandlesClearCommandChanged));
public static readonly DependencyProperty HandlesClearCommandProperty =
DependencyProperty.RegisterAttached("HandlesClearCommand", typeof(bool), typeof(ClearText), new PropertyMetadata(false, OnHandlesClearCommandChanged));

private static void OnHandlesClearCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
private static void OnHandlesClearCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is UIElement element)
{
if (d is UIElement element)
if ((bool)e.NewValue)
{
if ((bool)e.NewValue)
{
element.CommandBindings.Add(new CommandBinding(ClearCommand, OnClearCommand));
}
else
element.CommandBindings.Add(new CommandBinding(ClearCommand, OnClearCommand));
}
else
{
for (int i = element.CommandBindings.Count - 1; i >= 0; i--)
{
for (int i = element.CommandBindings.Count - 1; i >= 0; i--)
if (element.CommandBindings[i].Command == ClearCommand)
{
if (element.CommandBindings[i].Command == ClearCommand)
{
element.CommandBindings.RemoveAt(i);
}
element.CommandBindings.RemoveAt(i);
}
}
}
}

static void OnClearCommand(object sender, ExecutedRoutedEventArgs e)
static void OnClearCommand(object sender, ExecutedRoutedEventArgs e)
{
switch (sender)
{
switch (sender)
{
case DatePicker datePicker:
datePicker.SetCurrentValue(DatePicker.SelectedDateProperty, null);
break;
case TextBox textBox:
textBox.SetCurrentValue(TextBox.TextProperty, null);
break;
case ComboBox comboBox:
comboBox.SetCurrentValue(ComboBox.TextProperty, null);
comboBox.SetCurrentValue(Selector.SelectedItemProperty, null);
break;
case PasswordBox passwordBox:
passwordBox.Password = null;
break;
}
e.Handled = true;
case DatePicker datePicker:
datePicker.SetCurrentValue(DatePicker.SelectedDateProperty, null);
break;
case TextBox textBox:
textBox.SetCurrentValue(TextBox.TextProperty, null);
break;
case ComboBox comboBox:
comboBox.SetCurrentValue(ComboBox.TextProperty, null);
comboBox.SetCurrentValue(Selector.SelectedItemProperty, null);
break;
case PasswordBox passwordBox:
passwordBox.Password = null;
break;
}
e.Handled = true;
}

}

}