ContentControls 的 DataTemplate 默认可见性

发布于 2024-08-31 20:03:35 字数 1069 浏览 3 评论 0原文

在我基于 MVVM 的 WPF 应用程序中,我有很多不同的 ViewModel 类型,它们动态加载到 ContentControls 或 ContentPresenters 中。因此,我需要明确设置 XAML 中要使用的 DataTemplate:

现在我的问题是内容控件正在显示someTemplate 的 UI,即使 ContentControl 未绑定任何内容(即 ViewModel.SomePropertyOfTypeViewModel 为 null) 如果所有 ContentControl 当前未绑定任何内容,是否有一种快速简便的方法可以使它们不显示任何内容?当我使用隐式 DataTemplates 时,一切都会按预期工作。不幸的是我不能在这里使用这个机制。

更新:

我当前的解决方案是为每个 ViewModel 提供一个额外的 bool Visible 属性,该属性作为父 ViewModel 中的属性公开。仅当属性不为 null 时才返回 true。 ContentControl 的 Visiblibilty 绑定到此属性。 ParentViewModel.SomePropertyOfTypeViewModelVisible, ParentViewModel.SomeOtherPropertyOfTypeViewModelVisible ...

<ContentControl Content={Binding SomePropertyOfTypeViewModel} Visibility={Binding SomePropertyOfTypeViewModelVisible, Converter={StaticRresource boolToVisibiltyConverter}}" />

这不是很令人满意,因为我必须维护很多额外的属性。

In my MVVM based WPF application I have a lot of different ViewModel types that dynamically loaded into ContentControls or ContentPresenters. Therefor I need to explictly set what DataTemplate is to be used in XAML:

<ContentControl Content={Binding SomePropertyOfTypeViewModel} ContentTemplate={StaticResource someTemplate} />

Now my problem is that the content control is displaying the UI of someTemplate even if the ContentControl is bound to nothing (i.e ViewModel.SomePropertyOfTypeViewModel is null)
Is there a quick and easy way to make all ContentControls display nothing if they are currently bound to nothing? When I use implicit DataTemplates everything works as expected. Unfortunately I can't use this mechanism here.

Update:

My current solution is to have one extra bool Visible property for each ViewModel that is exposed as a property in the parent ViewModels. It returns true only when the the property is not null. The ContentControl's Visiblibilty is bound to this property.
ParentViewModel.SomePropertyOfTypeViewModelVisible, ParentViewModel.SomeOtherPropertyOfTypeViewModelVisible ...

<ContentControl Content={Binding SomePropertyOfTypeViewModel} Visibility={Binding SomePropertyOfTypeViewModelVisible, Converter={StaticRresource boolToVisibiltyConverter}}" />

This is not very satisfying because I have to maintain a lot of extra properties.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

滥情空心 2024-09-07 20:03:35

设置 ContentControl 的“可见性”可以解决您的问题吗?如果是这样,在您的 ViewModel 中,您可以为要绑定的 ContentControl 的可见性创建一个 Visibility 属性。在属性中,您可以检查 SomePropertyOfTypeViewModel 是否为 null。设置 SomePropertyOfTypeViewModel 时,您还需要通知 ContentControlVisibility 属性已更改。

<ContentControl Content={Binding SomePropertyOfTypeViewModel} Visibility={Binding ContentControlVisibility} />

public Visibility ContentControlVisibility
    {
        get
        {
            return SomePropertyOfTypeViewModel == null ? Visibility.Collapsed : Visibility.Visible;
        }
    }

Would setting the 'Visibility' of the ContentControl solve your problem? If so, in your ViewModel, you could create a Visibility property for the Visibility of the ContentControl to bind to. In the property, you could check to see if SomePropertyOfTypeViewModel is null. When setting the SomePropertyOfTypeViewModel, you would also want to notify that the ContentControlVisibility property changed.

<ContentControl Content={Binding SomePropertyOfTypeViewModel} Visibility={Binding ContentControlVisibility} />

public Visibility ContentControlVisibility
    {
        get
        {
            return SomePropertyOfTypeViewModel == null ? Visibility.Collapsed : Visibility.Visible;
        }
    }
凌乱心跳 2024-09-07 20:03:35

使用 TemplateSelector 似乎是最好的选择。

Using a TemplateSelector seems to be as good as it gets.

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