2
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();
    }
}

请检查转换器,如果autosize为 false ,我想返回矩形的原始高度。

4

2 回答 2

1

这似乎是一个首选的解决方案,所以我发布我的评论作为答案。

您可以使用多重绑定和IMultiValueConverter. 然后,您可以绑定 autosize 和原始高度值并在转换器中处理它。

查看此链接以获取有关多重绑定的更多信息:http: //blog.csainty.com/2009/12/wpf-multibinding-and.html

于 2012-11-27T20:15:24.067 回答
0

你应该考虑 IMultiValueConverter 而不是 IValueConverter

于 2012-11-27T20:12:09.697 回答