简体   繁体   English

使用按钮单击事件清除/擦除文本框内容

[英]Clear / Erase TextBox Content With Button Click Event

I have the following XAML snippet: 我有以下XAML代码段:

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

I'd like to erase the content of the TextBox when the user presses the Button. 我想在用户按下按钮时擦除TextBox的内容。

Of course doing so from Code Behind is a trivial task, but I wanted to accomplish it only through the use of XAML, and so using Triggers. 当然,从Code Behind执行此操作是一项微不足道的任务,但是我只想通过使用XAML以及使用Triggers来完成它。

I tried researching on the net, but I either found uncorrect solutions, or overly-convoluted solutions, so I'd like to hear some clean and compact solutions. 我尝试在网络上进行研究,但是发现了不正确的解决方案或过于复杂的解决方案,因此我想听听一些干净紧凑的解决方案。

Here I had a spare minute, hope this helps, cheers. 在这里,我有空闲时间,希望对您有所帮助。

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 either TargetName OR TargetObject but I included both for examples sake. 哦,还有PS-实际上,您只需要TargetNameTargetObject但是出于示例的TargetObject ,我将二者都包括在内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM