我正在創建一個包含三個屬性的簡單用戶控件。爲了簡單起見,我們假設這些是A,B和C.此外C = A + B。我想在文本框中顯示它們全部(A,B - 用戶可編輯,C - 只讀)。每當用戶修改A或B時,C的值應該被更新。將邏輯添加到WPF中的用戶控件中
我已經在MyControl.xaml.cs文件中爲A和B創建了依賴屬性。
public static readonly DependencyProperty AProperty =
DependencyProperty.Register("A", typeof(double),
typeof(MyControl), new FrameworkPropertyMetadata(0.0));
public double A
{
get { return (double)GetValue(AProperty); }
set { SetValue(AProperty, value); }
}
public static readonly DependencyProperty BProperty =
DependencyProperty.Register("B", typeof(double),
typeof(MyControl), new FrameworkPropertyMetadata(0.0));
public double B
{
get { return (double)GetValue(BProperty); }
set { SetValue(BProperty, value); }
}
我的問題是:我該怎麼處理C以及應該保留其定義?邏輯應該在控件內編碼還是用戶有責任記住這些屬性之間的關係?
謝謝!這正是我一直在尋找的。 –