ContentControls 的 DataTemplate 默认可见性
在我基于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
设置 ContentControl 的“可见性”可以解决您的问题吗?如果是这样,在您的 ViewModel 中,您可以为要绑定的 ContentControl 的可见性创建一个 Visibility 属性。在属性中,您可以检查 SomePropertyOfTypeViewModel 是否为 null。设置 SomePropertyOfTypeViewModel 时,您还需要通知 ContentControlVisibility 属性已更改。
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.
使用 TemplateSelector 似乎是最好的选择。
Using a TemplateSelector seems to be as good as it gets.