1 回答
TA贡献1860条经验 获得超8个赞
来自克莱门斯的建议。我删除了 INotifyPropertyChanged 接口,并将 PropertyChangedCallback 与重新计算 Sum 属性的 MyItem 属性一起使用。这似乎工作正常。
更新了 MyItemControl:
public partial class MyItemControl : UserControl
{
public MyItemControl()
{
InitializeComponent();
Sum = MyItem != null ? MyItem.Num1 + MyItem.Num2 + MyItem.Num3 : 0;
}
public MyItem MyItem
{
get { return (MyItem)GetValue(MyItemProperty); }
set { SetValue(MyItemProperty, value); }
}
public static readonly DependencyProperty MyItemProperty =
DependencyProperty.Register("MyItem", typeof(MyItem), typeof(MyItemControl), new PropertyMetadata(null, MyItemPropertyChanged));
private static void MyItemPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
MyItemControl sender = (MyItemControl)obj;
sender.Sum = sender.MyItem.Num1 + sender.MyItem.Num2 + sender.MyItem.Num3;
Console.WriteLine("Update sum");
}
public int Sum
{
get { return (int)GetValue(SumProperty); }
set { SetValue(SumProperty, value); }
}
private static readonly DependencyProperty SumProperty =
DependencyProperty.Register("Sum", typeof(int), typeof(MyItemControl), new PropertyMetadata(0));
}
- 1 回答
- 0 关注
- 152 浏览
添加回答
举报