WPF/MVVM:当 UserControl 后面的 ViewModel 尚未初始化时禁用按钮状态?
我有一个带有 ListBox 和 3 个按钮的 DocumentListView.Xaml。
在该 UserControl 后面是一个带有 3 个按钮的 DocumentListViewModel,它们的 Command 属性绑定到 3 个 RelayCommand。
我有 3 个控制器,如 AdministrationController、BillingController、ReportController。
每个控制器都有 ObservableCollections,例如 Customer 1 : N Order 1: N Document 与其他控制器相同。
在一个控制器中我有一种特殊的绑定情况。当我的 DocumentListViewModel 未像 OrderViewModel 一样由其父 ViewModel 初始化时(因为没有加载/存在订单),则我的 UserControl 有 3 个已启用的按钮。好吧,用户可以按 3 个按钮,但什么也没有发生,但仍然非常令人困惑,最重要的是,我的用户界面的一致性消失了。
如何将按钮的命令设置为默认为“禁用”?
将 Buttons IsEnabled 属性设置为 false 没有帮助,因为按钮将永远保持禁用状态。没有 CanExecute TRUE 会将其设置为 IsEnabled = true。
而且我不想引入另一个属性 IsButtonEnabled...那很愚蠢,因为我的按钮逻辑背后有两个世界 winforms 和 wpf...ICommand 应该足够了。
I have a DocumentListView.Xaml with a ListBox and 3 Buttons.
Behind that UserControl sits a DocumentListViewModel with 3 Buttons and their Command Property bound to 3 RelayCommands.
I have 3 Controller like AdministrationController, BillingController, ReportController.
Every Controller has ObservableCollections like Customer 1 : N Order 1: N Document same for the other Controller.
In one Controller I have a special binding situation. When my DocumentListViewModel is not initialized by its parent ViewModel like OrderViewModel (because no orders are loaded/exist) then my UserControl has 3 buttons which are ENABLED. Ok the user can press the 3 buttons and nothing happens but still its very confusing and above all the consistency in my user interface is gone.
How can I set the Command of a Button as default to "Disabled" ?
Setting the Buttons IsEnabled property to false does not help because the button will stay forever in the disabled state. No CanExecute TRUE will set it to IsEnabled = true.
AND I do not want to introduce another property IsButtonEnabled... that stupid because then I have both worlds winforms and wpf behind my buttons logic... ICommand should be enough.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
或者您可以使用按钮的样式来禁用:
Or you can use a Style for the button to disable:
这是一个有趣的情况。老实说,我从未遇到过 UI 已加载并可交互但 ViewModel 尚未绑定的情况。
但是,暂时忽略这一点,您可能会在绑定上使用 FallbackValue 来绑定到全局可用的 NullCommand 或始终为其 CanExecute 方法返回 false 的内容。
This is an interesting situation. Honestly I've never run into the case where the UI was loaded and interactive but the ViewModel was not yet bound.
However, ignoring that for a moment, you could potentially use a FallbackValue on your binding to bind to a globally available NullCommand or something that always returns false for its CanExecute method.