如何隐藏控制如果底层的DataContext为空? [英] How to hide a control if the underlying DataContext is null?
问题描述
我在我的视图模型的对象,有一堆的属性,其中一些偶尔会为空。我不想只是显示一些控件,如果这些特殊的控制是空。我怎么会去隐藏控件,如果绑定为空?我在想一些变频器,但不知道我怎么会去究竟这样做。任何想法?
I have an object in my view model that has a bunch of properties, some of them will occasionally be null. I don't want to just show some controls if these particular controls are null. How would I go about hiding the control if the bind is null? I was thinking of some sort of converter, but don't know how I'd go about doing it exactly. Any ideas?
编辑:对不起,我要指出,这也将是在Silverlight,所以我不知道,如果样式触发器会工作......
edit: sorry, I should mention that this will also be in Silverlight, so I'm not sure if Style triggers would work...?
推荐答案
有一个转换器像如下,
public sealed class NullToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value == null ? Visibility.Hidden: Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
现在,随着能见度属性的属性绑定为好。就像,
Now, bind the property with the Visibility property as well. Like,
<ListBox ItemsSource="{Binding Path=Squad}"
Visibility="{Binding Converter={StaticResource nullToVisibilityConverter},
Path=Squad}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
这篇关于如何隐藏控制如果底层的DataContext为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!