c# - 如何对 TextBox.Triggers(事件触发器)执行某些操作?

标签 c# wpf xaml

我必须禁用某个按钮。

如何为此使用 TextBox.Triggers

有 sample 吗?

谢谢回复!

最佳答案

我假设这与 your other question 有关关于根据 TextBox 是否存在验证错误触发 Button 上的 Enabled 属性。

如果是这样,您将使用 DataTrigger 来测试 TextBox.Validation.HasError 属性以查看它是否有任何错误,如果有则禁用 Button

<Style x:Key="MyButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="IsEnabled" Value="True" />
    <DataTrigger Binding="{Binding ElementName=MyTextBox, Path=Validation.HasError" Value="True">
        <Setter Property="IsEnabled" Value="False"/>
    </DataTrigger>
</Style>

请确保将您的 TextBox 与 ValidatesOnDataErrors="True" 绑定(bind)以使其工作

<TextBox x:Name="MyTextBox" Text="{Binding SomeText, ValidatesOnDataErrors=True }" />

作为旁注,我对您的其他问题的评论在这里仍然适用。我会亲自在您的 ViewModel 中实现 IDataErrorInfo 并使 SaveCommand.CanExecute() 仅在 ViewModel.IsValid 时返回 true >。如果 SaveCommand 不应该执行,它将自动处理禁用按钮

关于c# - 如何对 TextBox.Triggers(事件触发器)执行某些操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8977422/

相关文章:

c# - Windows 窗体 C# 通过代码更改用户控件

c# - 空日期时间变量

wpf - WPF .NET 4.0 的刷新问题 - 黑色垂直线

wpf - 在 WPF 中将 Combobox 与大量数据绑定(bind)

c# - 将颜色属性从 xaml 绑定(bind)到 c#

c# - 长时间运行的阻塞方法。阻塞、休眠、开始/结束和异步之间的区别

c# - C#HttpWebRequest的错误处理

.net - 在运行时向 WPF TreeViewItem 添加图标

c# - Xamarin.Forms.Xaml.XamlParseException : MarkupExtension not found

silverlight - 是否可以绑定(bind)到 Silverlight 中的 lambda 表达式?