wpf NullToVisibilityConverter 如果不为空则使可见

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21939667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:58:50  来源:igfitidea点击:

NullToVisibilityConverter make visible if not null

c#wpfxamlmvvm

提问by Roar

Want to hide and show property grid for SelectedItemin listview

想要在列表视图中隐藏和显示SelectedItem 的属性网格

<UserControl xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
      <ListView>
         <!--here is list view-->
      </ListView>
      <xctk:PropertyGrid SelectedObject="{Binding Active}" Visibility="{Binding Active, Converter=NullToVisibilityConverter}" >  
   </xctk:PropertyGrid>
</UserControl>

So I need converter and use it in visibility property converter. Any help?

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

回答by 123 456 789 0

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();
    }
}

Then reference the NullVisibilityConverter in your XAML Resources.

然后在 XAML 资源中引用 NullVisibilityConverter。

<StackPanel.Resources>
  <simpleXamlContent:NullVisibilityConverter x:Key="NullToVisibilityConverter"/>
</StackPanel.Resources>

回答by WPFUser

To use the converter we can create one in the resources, and refer to it as a static resource in the binding statement.

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

<UserControl xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit">
    <UserControl.Resources>
      <yournamespace:NullVisibilityConverter x:Key="NullToVisibilityConverter"/>
    </UserControl.Resources>

    <ListView>
    <!--here is list view-->
    </ListView>

    <xctk:PropertyGrid SelectedObject="{Binding Active}" Visibility="{Binding Active, Converter={StaticResource NullToVisibilityConverter}}" >  
    </xctk:PropertyGrid>
</UserControl>

and Converter class itself

和转换器类本身

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();
    }
}

回答by ApceH Hypocrite

There is little more useful version allows to set default invisibility value:

有一些更有用的版本允许设置默认的隐身值:

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;
}

Where ever in resources add:

在资源中的任何地方添加:

<converters:NullReferenceToVisibilityConverter x:Key="NullToVis" />

And use it like there:

并像那里一样使用它:

<StackPanel Visibility="{Binding MyObject, Converter={StaticResource NullToVis}}">
<StackPanel Visibility="{Binding MyObject, Converter={StaticResource NullToVis}, ConverterParameter=Hidden}">