Rectangle rectangle = new Rectangle();
rectangle.StrokeThickness = 10;
rectangle.Height = 200;
rectangle.Width = 100;
//Self defined propety
Boolean AutoSize = false;
rectangle.DataContext = AutoSize;
//Add binding
Binding bind = new Binding(rectangle.DataContext);
bind.Mode = BindingMode.OneWay;
bind.Converter = ConvertAutoSize2Height;
bindingList.Add(bind);
canvas.Children.Insert(0, rectangle);
//Value converter
[ValueConversion(typeof(Boolean), typeof(Double))]
public class ConvertAutoSize2Height : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Boolean autoSize = (Boolean)value;
if (autoSize)
return Double.NaN;
else
return **<<<I wanna return original height if autosize is false>>>**;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Please check the converter, I wanna return the rect’s original height if autosize is false.
This seems to be a preferred solution, so I’m posting my comment as an answer.
You may be able to achieve this using multibinding and
IMultiValueConverter. You could then bind both autosize and the original height value and process it in the converter.Take a look at this link for more information about multibinding: http://blog.csainty.com/2009/12/wpf-multibinding-and.html.