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();
}
}
請檢查轉換器,我想退掉矩形的原始高度,如果自動調整是假的。如何從轉換器獲得依賴項屬性數據綁定
您不能創建存儲高度原始值的自動屬性或屬性。 – MethodMan
@DJKRAZE那麼如何在轉換器中檢索它? – dongx
您可以使用多重綁定和「IMultiValueConverter」來實現此目的。然後,您可以綁定autosize和原始高度值並在轉換器中對其進行處理。看看這個鏈接瞭解更多關於multibinding的信息:http://blog.csainty.com/2009/12/wpf-multibinding-and.html。讓我知道這是否有幫助。 –