我有一个类可以即时计算它的属性,例如:
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 中有 2 个文本框,一个将 TwoWay 绑定到 Radius,另一个将 OneWay 绑定到 Area。
我应该怎么做才能对 Radius 的文本框进行编辑更新区域的文本框?