当 ItemsSource 为空或 null 时,MVVM WPF 用 Label 替换 ListBox
我在 WPF (.NET 3.5) 中有这一行:
<ListBox ItemsSource="{Binding Locks}" Style="{DynamicResource FancyListBox}" />
我期望的行为是,当属性 Locks(ObservableCollection)为 null 或包含 0 个元素时,显示如下所示的标签而不是 ListBox。
<Label Content="No locks are present" Style="{DynamicResource FancyLabel}" />
到目前为止,我能想到的唯一解决方案是使用值转换器(即像 CollectionToVisibilityShowIfNull、CollectionToVisibilityShowIfNotNull 这样的转换器)将 Locks 属性绑定到可见性,但我不确定这是否是最佳解决方案。
... Visibility={Binding Locks, Converter={StaticResource CollectiontoVisibilityShowIfNull}} ..
感谢您的帮助!
I have this line in WPF (.NET 3.5) :
<ListBox ItemsSource="{Binding Locks}" Style="{DynamicResource FancyListBox}" />
My desired behavior is that when the property Locks (an ObservableCollection) is either null or contains 0 elements, show a label like the one below instead of the ListBox.
<Label Content="No locks are present" Style="{DynamicResource FancyLabel}" />
The only solution I can think of so far is to bind the Locks property to visibility using a valueconverter (i.e. converters like CollectionToVisibilityShowIfNull, CollectionToVisibilityShowIfNotNull), but I am not sure if that is the best solution.
... Visibility={Binding Locks, Converter={StaticResource CollectiontoVisibilityShowIfNull}} ..
Thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我通常做的事情:
Here's what I usually do: