Caliburn.Micro 设置控件状态

发布于 2025-01-08 16:43:16 字数 97 浏览 4 评论 0原文

我尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木槿暧夏七纪年 2025-01-15 16:43:16

是的,使用绑定。在 XAML 中,设置 Visibility 属性以绑定到视图模型上的公共属性,并使用标准 BooleanToVisibilityConverter 将布尔视图模型公共属性转换为有效的 可见性值。

如果您希望创建一个通用解决方案以允许配置任何视图元素的可见性,那么一种选择是插入 Caliburn.Micro 绑定过程。例如,您可以将另一个元素添加到 ViewModelBinder.BindProperties 调用列表中。

我还没有对此进行测试,但这里这是在引导程序的 Configure 方法中完成的:

protected override void Configure()
{
    ViewModelBinder.BindProperties += (namedElements, viewModelType) =>
    {                    
        foreach (var element in namedElements)
        {
            // Generate a unique identifier for an element on a particular view
            string uniqueElementId = string.Format("{0}.{1}", viewModelType.FullName, element.Name);

            // Calculate the visibility of the element based on unique element id
            bool elementVisible = ...

            // Set the element's visibility
            element.Visibility = elementVisible ? Visibility.Visible : Visibility.Collapsed;
        }
    };

   ... other configuration code
}

BindProperties public Func 获取所有属性的集合视图上命名元素的名称,以及绑定到的视图模型的类型。您可以使用此信息来唯一标识每个视图控件,然后根据该唯一标识符计算数据的可见性。

请注意,在类似这样的代码中设置每个元素的 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 standard BooleanToVisibilityConverter to convert your boolean view model public property to a valid Visibility 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:

protected override void Configure()
{
    ViewModelBinder.BindProperties += (namedElements, viewModelType) =>
    {                    
        foreach (var element in namedElements)
        {
            // Generate a unique identifier for an element on a particular view
            string uniqueElementId = string.Format("{0}.{1}", viewModelType.FullName, element.Name);

            // Calculate the visibility of the element based on unique element id
            bool elementVisible = ...

            // Set the element's visibility
            element.Visibility = elementVisible ? Visibility.Visible : Visibility.Collapsed;
        }
    };

   ... other configuration code
}

The BindProperties public Func 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 the Visibility property in XAML.

静谧 2025-01-15 16:43:16

我不知道你到底想通过这个达到什么目的。尽管如此,使用(和更改)视图的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文