Caliburn.Micro 设置控件状态
我尝试在 OnActivate 方法中设置属于视图模型的所有控件的启用/可见性状态。到目前为止,这没有问题,但我不确定是否有更好的方法可以在不从视图模型访问视图的情况下执行此操作。
I try to set the enabled/visibility status of all my controls which belong to a viewmodel inside the OnActivate method. So far this is no problem but I'm not sure if there is a better approach to do this without accessing the view from the viewmodel.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,使用绑定。在 XAML 中,设置
Visibility
属性以绑定到视图模型上的公共属性,并使用标准BooleanToVisibilityConverter
将布尔视图模型公共属性转换为有效的可见性
值。如果您希望创建一个通用解决方案以允许配置任何视图元素的可见性,那么一种选择是插入 Caliburn.Micro 绑定过程。例如,您可以将另一个元素添加到
ViewModelBinder.BindProperties
调用列表中。我还没有对此进行测试,但这里这是在引导程序的
Configure
方法中完成的:BindProperties
publicFunc
获取所有属性的集合视图上命名元素的名称,以及绑定到的视图模型的类型。您可以使用此信息来唯一标识每个视图控件,然后根据该唯一标识符计算数据的可见性。请注意,在类似这样的代码中设置每个元素的
Visibility
将覆盖 XAML 中Visibility
属性上存在的任何绑定。Yes, use binding. In XAML, set the
Visibility
property to bind to a public property on your view model, and use the standardBooleanToVisibilityConverter
to convert your boolean view model public property to a validVisibility
value.If you wish to create a general solution to allow configurability of the visibility of any view element, then one option is to plug into the Caliburn.Micro binding process. For example, you can add another element to the
ViewModelBinder.BindProperties
invocation list.I haven't tested this, but here this is done in the
Configure
method of your bootstrapper:The
BindProperties
publicFunc
gets a collection of all of the named elements on your view, as well as the Type of the view model that is being bound to. You can use this information to uniquely identify each view control, and then calculate the visibility from your data based on that unique identifier.Note that setting the
Visibility
of each element in code like this will override any bindings that are present on theVisibility
property in XAML.我不知道你到底想通过这个达到什么目的。尽管如此,使用(和更改)视图的 VisualState 或 CM 防护方法可能会起作用。
I don't know what exactly you want to achieve by this. Nevertheless, using (and changing) VisualState of the view or C.M. guard methods might do the work.