Created
June 3, 2021 03:58
-
-
Save emoacht/3be0e5d439e67fd9acca05496cb77826 to your computer and use it in GitHub Desktop.
Converter to change Grid.Column or Grid.Row utilizing FallbackValue (default value) and ConverterParameter (changed value)
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
public class BooleanToIntConverter : IValueConverter | |
{ | |
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) | |
{ | |
return ((value is true) && int.TryParse(parameter?.ToString(), out int buffer)) | |
? buffer | |
: DependencyProperty.UnsetValue; | |
} | |
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) | |
{ | |
throw new NotImplementedException(); | |
} | |
} |
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
<Grid> | |
<Grid.ColumnDefinitions> | |
<ColumnDefinition/> | |
<ColumnDefinition/> | |
</Grid.ColumnDefinitions> | |
<Rectangle Grid.Column="{Binding IsSwapped, Converter={StaticResource BooleanToIntConverterKey}, | |
ConverterParameter=1, FallbackValue=0}" | |
Width="200" Height="200" | |
Fill="Red"/> | |
<Rectangle Grid.Column="{Binding IsSwapped, Converter={StaticResource BooleanToIntConverterKey}, | |
ConverterParameter=0, FallbackValue=1}" | |
Width="200" Height="200" | |
Fill="Blue"/> | |
</Grid> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment