有没有最好的方法来手动禁用和重新启用绑定按钮?

时间:2013-05-13 12:04:43

标签: c# .net wpf xaml data-binding

在我的ViewModel中,我有以下方法:

public async Task<Boolean> DoSomething(Button sender)
{
    Binding binding = sender.GetBindingExpression(Button.IsEnabledProperty).ParentBinding;

    sender.IsEnabled = false;

    DoFastStuffs();

    await Task.Delay(250);

    sender.SetBinding(Button.IsEnabledProperty, binding);

    return true;
}

有更好的方法吗?因为我注意到手动设置属性而不保存绑定在某处永远删除了XAML对象的实际绑定。

编辑:这是我的按钮:

<Button IsEnabled="{Binding Converter={StaticResource ObjectToBooleanConverter}, ElementName=ComboBox, Path=SelectedItem}"/>

1 个答案:

答案 0 :(得分:1)

为什么不绑定按钮的Command属性。实现ICommand还将为您提供UI控件的启用/禁用操作(几乎)免费使用CanExecute()功能来检查ComboBox的选定项目......

相关问题