Created
November 3, 2017 19:49
-
-
Save juanpaexpedite/ec86478f2cf16e91cc47998f9c9f34d2 to your computer and use it in GitHub Desktop.
WPF Converter with binding 1/2
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 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