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の場合、rect の元の高さを返したいので、コンバーターを確認してください。

4

2 に答える 2

0

IValueConverter の代わりに IMultiValueConverter を検討する必要があります

于 2012-11-27T20:12:09.697 に答える