Skip to content

Instantly share code, notes, and snippets.

@emoacht
Created June 3, 2021 03:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emoacht/3be0e5d439e67fd9acca05496cb77826 to your computer and use it in GitHub Desktop.
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)
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();
}
}
<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