【问题标题】:How to make button bound to ICommand enable/disable when text changes, as opposed to losing focus of TextBox如何在文本更改时启用/禁用绑定到 ICommand 的按钮,而不是失去 TextBox 的焦点
【发布时间】:2014-09-04 16:40:58
【问题描述】:

我有一个带有“保存”按钮的 Windows 应用商店 XAML 应用程序,该按钮指向我的 ViewModel 中的 ICommand 属性。

<Button Content="Save" 
    Command="{Binding DataContext.SaveNewNote, ElementName=grid, Mode=OneWay}"
    CommandParameter="{Binding DataContext.CurrentItem, ElementName=grid}" />

在我的 Note 模型的 propertychanged 事件上,它会触发 CanExecutedChanged 事件 - 每次(每次击键)。但是,只有当我离开相关文本框之一的焦点时,此按钮才会启用/禁用。

换句话说,有一个“名称”文本框。如果 String.IsNullOrWhiteSpace(Name.Text),则 CanExecute 设置为 false。这似乎在每次击键时都会执行。

我希望看到这个绑定的保存按钮在每次击键时启用和禁用。但是,相反,只有当我跳出文本框时,我才会看到按钮启用/禁用。

如何让这个绑定按钮在每次击键时尊重 CanExecuteChanged 事件,而不是在 texbox 失去焦点时?

【问题讨论】:

标签: xaml mvvm winrt-xaml icommand


【解决方案1】:

@will 和 @patrick 在原帖中的 cmets 都有正确答案。为此,您必须将“UpdateSourceTrigger”设置为“PropertyChanged”并给出预期结果。

<TextBox Grid.Row="1" PlaceholderText="Enter a short description here" 
    Text="{Binding NoteAbstract, Mode=TwoWay, 
    UpdateSourceTrigger=PropertyChanged}">
</TextBox>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多