Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullToVisibilityConverter make visible if not null

Tags:

c#

mvvm

wpf

xaml

Want to hide and show property grid for SelectedItem in listview

<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?

like image 935
Roar Avatar asked Feb 21 '14 16:02

Roar


3 Answers

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.

<StackPanel.Resources>
  <simpleXamlContent:NullVisibilityConverter x:Key="NullToVisibilityConverter"/>
</StackPanel.Resources>
like image 179
123 456 789 0 Avatar answered Oct 24 '22 10:10

123 456 789 0


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();
    }
}
like image 27
WPFUser Avatar answered Oct 24 '22 09:10

WPFUser


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}">
like image 4
ApceH Hypocrite Avatar answered Oct 24 '22 10:10

ApceH Hypocrite