【问题标题】:Clear / Erase TextBox Content With Button Click Event使用按钮单击事件清除/擦除文本框内容
【发布时间】:2018-01-07 03:31:37
【问题描述】:

我有以下 XAML sn-p:

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

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

当然,从 Code Behind 这样做是一项微不足道的任务,但我只想通过使用 XAML 来完成它,因此使用触发器。

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

【问题讨论】:

  • 我认为你需要Command 而不是触发器。

标签: c# .net wpf xaml triggers


【解决方案1】:

在这里我有空闲时间,希望这会有所帮助,干杯。

命名空间;

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,但为了举例,我将两者都包括在内。

【讨论】:

    猜你喜欢
    • 2014-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    相关资源
    最近更新 更多