如果不为null,则NullToVisibilityConverter变为可见

想要在listview中隐藏和显示SelectedItem的属性网格

<UserControl xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"       

所以我需要转换器并在可见性属性转换器中使用它。 有帮助吗?

 public class NullVisibilityConverter : 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(); } } 

然后在XAML资源中引用NullVisibilityConverter。

    

要使用转换器,我们可以在资源中创建一个,并在绑定语句中将其称为静态资源。

           

和转换器一样

 public class NullVisibilityConverter : 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(); } } 

没有更多有用的版本允许设置默认的不可见性值:

 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { string defaultInvisibility = parameter as string; Visibility invisibility = (defaultInvisibility != null) ? (Visibility)Enum.Parse(typeof(Visibility), defaultInvisibility) : Visibility.Collapsed; return value == null ? invisibility : Visibility.Visible; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return DependencyProperty.UnsetValue; } 

在哪里资源添加:

  

并在那里使用它: