Visibility Convert

Apr 3, 2012 at 12:09 PM

How can i convert Visibility.Collapsed to Visibility.Visible and Visibility.Visible to Visibility.Collapsed with this dll.

Apr 3, 2012 at 9:20 PM
Edited Apr 3, 2012 at 9:25 PM

Your simplest bet is to use the MapConverter.

Should you have any problems or ideas, don't be a jerk keeping it to yourself but come back to share it with me!

<Control
    xmlns:boo="http://schemas.kent.boogaart.com/converters"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    Background="Red" Height="50" Width="50">
  <Control.Visibility>
    <Binding Path="Visibility">
      <Binding.Converter>
        <boo:MapConverter>
          <boo:Mapping To="Visible">
            <boo:Mapping.From>
              <Visibility>Collapsed</Visibility>
            </boo:Mapping.From>
          </boo:Mapping>
          <boo:Mapping To="Collapsed">
            <boo:Mapping.From>
              <Visibility>Visible</Visibility>
            </boo:Mapping.From>
          </boo:Mapping>
        </boo:MapConverter>
      </Binding.Converter>
    </Binding>
  </Control.Visibility>
</Control>
Apr 4, 2012 at 12:43 PM

Thank you for your response. I'm using this but it is not very fast to write. Maybe some common convertions can be put in dll. For example,

bool to visibilityC true <=> Visibility.Visible     false <=> Visibility.Collapsed

bool to visibilityH true <=> Visibility.Visible     false <=> Visibility.Hidden

bool to inverseVisibilityC true <=> Visibility.Collapsed     false <=> Visibility.Visible

bool to inverseVisibilityH true <=> Visibility.Hidden     false <=> Visibility.Visible

bool to inverseBool true <=> false false <=> true

Apr 4, 2012 at 6:53 PM

You can create such a converter once in your application/control resources with a key, then refer to it as a StaticResource:

<Control Visibility="{Binding IsVisible, Converter={StaticResource VisibilityConverter}}" />

Another option is to subclass the MapConverter/Binding, but then you would be better off implementing directly from IValueConverter.

A third option (which can also be combined witht the above), you can inherit from MarkupExtension (or IMarkupExtension<T> in SL5), and use it as a xaml markup extension, read this blog post for further details (applicable in WPF/SL5).