Created
April 7, 2011 08:40
-
-
Save timoi/907348 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// a generic wpf boolean value converter class | |
public class GenericBooleanConverter : IValueConverter | |
{ | |
public object TrueValue { get; set; } | |
public object FalseValue { get; set; } | |
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) | |
{ | |
return TypeDescriptor.GetConverter(targetType) | |
.ConvertFrom((bool)value ? TrueValue : FalseValue); | |
} | |
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) | |
{ | |
var convertedTrueValue = TypeDescriptor | |
.GetConverter(targetType).ConvertFrom(TrueValue); | |
return convertedTrueValue.Equals(value); | |
} | |
} | |
<!-- usage --> | |
<GenericBooleanConverter x:Key="booleanToVisibility" TrueValue="Visible" FalseValue="Collapsed" /> | |
<GenericBooleanConverter x:Key="boolToOrientation" TrueValue="Vertical" FalseValue="Horizontal" /> | |
<StackPanel | |
Orientation="{Binding IsMyViewModelVertical, Converter={StaticResource booleanToOrientation}}" | |
Visibility="{Binding IsMyViewModelVisible, Converter={StaticResource booleanToVisibility}}"> | |
<Label>I am a label that is sometimes visible</Label> | |
<Label>I am a label that is sometimes to the left and sometimes below the previous label</Label> | |
</StackPanel> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment