4
我有一個類,它計算它的財產上即時,例如:在即時DependencyProperties
class CircleArea
{
public double Radius { get; set; }
public double Area
{
get
{
return Radius * Radius * Math.PI;
}
}
}
我讓一個DependencyObject的做:
class CircleArea:
DependencyObject
{
public static readonly DependencyProperty RadiusProperty =
DependencyProperty.Register("Radius", typeof(double), typeof(CircleArea));
public double Radius
{
get { return (double)GetValue(RadiusProperty); }
set
{
SetValue(RadiusProperty, value);
CoerceValue(AreaProperty);
}
}
internal static readonly DependencyPropertyKey AreaPropertyKey =
DependencyProperty.RegisterReadOnly("Area", typeof(double), typeof(CircleArea), new PropertyMetadata(double.NaN));
public static readonly DependencyProperty AreaProperty = AreaPropertyKey.DependencyProperty;
public double Area
{
get
{
return Radius * Radius * Math.PI;
}
}
}
然後我在XAML中有兩個文本框,一個使用TwoWay綁定到Radius,另一個使用OneWay綁定到區域。
我該如何編輯Radius的文本框來更新Area的文本框?
我嘗試過'CoerceValue(AreaProperty);'和'InvalidateProperty(AreaProperty);',但它們都不起作用。 –