c# – 当没有当前项目时,如何禁用绑定到当前项目的ICommand的按
假设你有一个按钮,其命令属性绑定到某些集合的当前项目的一些ICommand.
当集合为空时,该按钮保持启用状态,并且点击它似乎是无效的.我想要的是,该按钮仍然禁用.我收集以下内容后,只要收集为空,就可以禁用按钮.然而,对于可能在更自然,更简单和更多的MVVM中可能实现的东西,似乎有点太复杂了. 因此,问题是:有一个更简单的方法来保持该按钮禁用,理想情况下,哪里没有使用代码隐藏? 的.xaml: <Button Content="Do something" > <Button.Command> <PriorityBinding> <Binding Path="Items/DoSomethingCmd" /> <Binding Path="DisabledCmd" /> </PriorityBinding> </Button.Command> </Button> 的.cs: public class ViewModel : NotificationObject { ObservableCollection<Foo> _items; public DelegateCommand DisabledCmd { get; private set; } public ObservableCollection<Foo> Items { get { return _items; } set { _items = value; RaisePropertyChanged("Items"); } } public ViewModel() { DisabledCmd = new DelegateCommand(DoNothing,CantDoAnything); } void DoNothing() { } bool CantDoAnything() { return false; } } 编辑: 一些注释: >我知道我可以使用lambda表达式,但在这个例子中我没有代码. 另一个编辑: 所以我基本上把这个答案作为一个解决方案:WPF/MVVM: Disable a Button’s state when the ViewModel behind the UserControl is not yet Initialized? 我相信,这是什么hbarck提出的. 解决方法
我会做类似于akjoshi,只有我会使用一个普通的Trigger而不是DataTrigger,我会检查Button.Command为null.因为禁用没有命令的按钮(特别是在没有点击事件处理程序的MVVM中)总是有意义的,将这个触发器包含在Button的默认样式中也是一个好主意,为了有这个行为在应用程序的所有按钮上…我没有看到使用虚拟命令的原因.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |