使用按钮单击事件清除/擦除文本框内容 [英] Clear / Erase TextBox Content With Button Click Event

查看:69
本文介绍了使用按钮单击事件清除/擦除文本框内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 XAML 片段:

I have the following XAML snippet:

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

我想在用户按下按钮时擦除 TextBox 的内容.

I'd like to erase the content of the TextBox when the user presses the Button.

当然,从代码隐藏中这样做是一项微不足道的任务,但我只想通过使用 XAML 来完成它,因此使用触发器.

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.

我尝试在网上研究,但我发现了不正确的解决方案,或者过于复杂的解决方案,所以我想听听一些简洁紧凑的解决方案.

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.

命名空间;

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

而且很容易.

<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>

哦,还有 P.S.- 你真的只需要 TargetNameTargetObject 但我把这两个都包括了作为例子.

Oh, and P.S. - You really only need either TargetName OR TargetObject but I included both for examples sake.

这篇关于使用按钮单击事件清除/擦除文本框内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆