在 MVVM 中单击按钮时清除 TextBox 的文本
我正在开发一个搜索框解决方案,其中没有绑定文本,因为我使用 Text
属性作为搜索命令的参数。 那部分工作得很好。
现在我想使用 TextBox
末尾设置的“清除按钮”清除 TextBox
中的文本。
我尝试使用 EventTrigger
,但 StoryBoard
阻止 Command
并且不会将文本设置为零。
下面是我的代码。 有什么想法吗?
<!--Search Box-->
<Grid Grid.Column="0">
<TextBox x:Name="FilterText">
<TextBox.InputBindings>
<KeyBinding Key="Enter" Command="{Binding FilterDataCommand}" CommandParameter="{Binding ElementName=FilterText, Path=Text}"/>
</TextBox.InputBindings>
</TextBox>
<Button HorizontalAlignment="Right" Width="20" Height="20" Margin="5,0" Style="{StaticResource ButtonTransparentStyle}" Command="{Binding ClearSearchCommand}">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<StringAnimationUsingKeyFrames Storyboard.TargetName="FilterText" Storyboard.TargetProperty="Text">
<DiscreteStringKeyFrame KeyTime="0:0:1" Value=""/>
</StringAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
<Image Source="/VisionRanger;component/Images/Icons/icon.clear.dark.png" Stretch="UniformToFill" Margin="4,3,2,4" Opacity="0.5"/>
</Button>
</Grid>
<!--Search Button-->
<Button Grid.Column="1" Style="{StaticResource ButtonTransparentStyle}" Margin="5,0,0,0" Width="25" HorizontalAlignment="Left"
Command="{Binding FilterDataCommand}" CommandParameter="{Binding ElementName=FilterText, Path=Text}">
<Image Source="/VisionRanger;component/Images/Icons/icon.search.dark.png" Stretch="UniformToFill"/>
</Button>
I'm working on a search box solution where there is no binded text as I'm using the Text
property as parameter for the search command.
And that part works just fine.
Now I would like to clear the text in the TextBox
using a "clear button" set at the end of the TextBox
.
I tried with EventTrigger
, but the StoryBoard
block the Command
and does not set the text to nil.
Below is my code.
Any idea?
<!--Search Box-->
<Grid Grid.Column="0">
<TextBox x:Name="FilterText">
<TextBox.InputBindings>
<KeyBinding Key="Enter" Command="{Binding FilterDataCommand}" CommandParameter="{Binding ElementName=FilterText, Path=Text}"/>
</TextBox.InputBindings>
</TextBox>
<Button HorizontalAlignment="Right" Width="20" Height="20" Margin="5,0" Style="{StaticResource ButtonTransparentStyle}" Command="{Binding ClearSearchCommand}">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<StringAnimationUsingKeyFrames Storyboard.TargetName="FilterText" Storyboard.TargetProperty="Text">
<DiscreteStringKeyFrame KeyTime="0:0:1" Value=""/>
</StringAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
<Image Source="/VisionRanger;component/Images/Icons/icon.clear.dark.png" Stretch="UniformToFill" Margin="4,3,2,4" Opacity="0.5"/>
</Button>
</Grid>
<!--Search Button-->
<Button Grid.Column="1" Style="{StaticResource ButtonTransparentStyle}" Margin="5,0,0,0" Width="25" HorizontalAlignment="Left"
Command="{Binding FilterDataCommand}" CommandParameter="{Binding ElementName=FilterText, Path=Text}">
<Image Source="/VisionRanger;component/Images/Icons/icon.search.dark.png" Stretch="UniformToFill"/>
</Button>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Text
属性不可动画化,请参阅文档中的备注。在参考源中,FrameworkPropertyMetadata
初始化为isAnimationProhibited
设置为true
。解决方案是将
MyFilterTextProperty
属性绑定到TwoWay
模式下TextBox
的Text
属性(即默认值)。然后在命令
ClearSearchCommand
中,您可以清除MyFilterTextProperty
。另一种方法是使用自定义
TriggerAction
清除TextBox
。安装Microsoft.Xaml.Behaviors.Wpf NuGet 包。创建一个像这样的自定义触发操作。它公开了TextBox
可以绑定到的依赖属性Target
。如果发生事件,则会将其清除。在 XAML 中,您只需附加行为并绑定
FilterText
必须包含 XAML bahviors 的
b
XML 命名空间。The
Text
property is not animatable, see Remarks in the documentation. In the reference source, theFrameworkPropertyMetadata
is initialized withisAnimationProhibited
set totrue
.A solution would be to bind a
MyFilterTextProperty
property to theText
property of theTextBox
inTwoWay
mode (which is the default).Then in your command
ClearSearchCommand
you could clearMyFilterTextProperty
.An alternative is to clear the
TextBox
using a customTriggerAction
. Install the Microsoft.Xaml.Behaviors.Wpf NuGet package. Create a custom trigger action like this. It exposes a dependency propertyTarget
that theTextBox
can be bound to. If an event occurs, it clears it.In XAML you simply attach the behavior and bind
FilterText
The
b
XML namespace for XAML bahviors must be included.对我来说最明显的方法是创建一个自定义控件。但我需要一些可以快速用于客户项目的东西,因此我创建了一种样式,利用按钮的属性来创建搜索“按钮”(当然还涉及其他样式),但它们不会改变基本原则用过的。
当有文本时,单击关闭按钮会将其清除。
在代码中的任何一点,我定义一个按钮并在其上调用此样式,并绑定其命令。
我相信有更好的方法可以做到这一点,但到目前为止这对我有用。
The obvious way for me is to create a custom control. But I needed something that I could use quickly for a client's project so I created a style that leveraged the properties of a button to create a search "button" (Of course there were other styles involved) but they don't change the underlying principle used.
And when there is text, clicking the close button will clear it.
At any point in code, I define a button and call this style on it, and bind its command.
I believe there are better ways to do this but this has worked for me by far.