Clear / Erase TextBox Content Using Click Event Button

I have the following XAML snippet:

<TextBox x:Name="FilterTB" TextChanged="FilterTB_TextChanged"/> <Button x:Name="CancelFilterSelectionButton" FontWeight="Bold" Content="X"/> 

I want to delete the contents of the TextBox when the user clicks the button.

Of course, doing this from Code Behind is a trivial task, but I wanted to do it only with XAML and thus with Triggers.

I tried to research on the net, but I either found the wrong solutions or overly confusing solutions, so I would like to hear some clean and compact solutions.

+1
source share
1 answer

Here I had a free minute, I hope this helps, welcomes.

Namespaces;

 xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:ei="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions" 

and easy peasy.

 <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center"> <TextBox x:Name="ThatThangToClear" Width="250"/> <Button x:Name="ClearThatThang" Content="Clear That Thang" Margin="5,0"> <i:Interaction.Triggers> <i:EventTrigger EventName="Click"> <ei:ChangePropertyAction TargetName="ThatThangToClear" TargetObject="{Binding ElementName=ThatThangToClear}" PropertyName="Text" Value="{x:Null}"/> </i:EventTrigger> </i:Interaction.Triggers> </Button> </StackPanel> 

Oh and PS - You really only need TargetName OR TargetObject , but I have included both for example.

+3
source

Source: https://habr.com/ru/post/1238431/


All Articles