Skip to content

Instantly share code, notes, and snippets.

@juanpaexpedite
Created November 3, 2017 19:49
Show Gist options
  • Save juanpaexpedite/ec86478f2cf16e91cc47998f9c9f34d2 to your computer and use it in GitHub Desktop.
Save juanpaexpedite/ec86478f2cf16e91cc47998f9c9f34d2 to your computer and use it in GitHub Desktop.
WPF Converter with binding 1/2
public class ZIndexConverter : DependencyObject, IValueConverter
{
public string Size
{
get { return (string)GetValue(SizeProperty); }
set { SetValue(SizeProperty, value); }
}
public static readonly DependencyProperty SizeProperty =
DependencyProperty.Register(nameof(Size), typeof(string), typeof(ZIndexConverter), new PropertyMetadata("0,0",OnSizeChanged));
private static void OnSizeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is ZIndexConverter instance)
{
instance.UpdateHeight();
}
}
private double height = 0;
private void UpdateHeight()
{
if (Size.Contains(',') && Size.Split(',') is string[] values && values.Length == 2)
{
bool px = double.TryParse(values[0], out double nx);
bool py = double.TryParse(values[1], out double ny);
if (py)
{
height = ny;
}
}
else
{
height = 0;
}
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if(value is string zindex)
{
bool ok = double.TryParse(zindex, out double newz);
if(ok)
{
return height - newz;
}
}
return height;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment