猿问

用户控件不更新某些属性

我有一个名为 MyItem 的类,它有一个 ID 属性,以及三个随机数属性。我有一个名为 MyItemControl 的 UserControl,它旨在显示 MyItem 的 ID、三个随机数以及这三个随机数的总和。我在 ListBox 中显示 MyItem 的集合,并且我有一个 contentcontrol,它通过 MyItemControl UserControl 显示有关 ListBox 所选 MyItem 的更多详细信息。


However, I run into a problem where, when a MyItem is selected, the contentcontrol only updates the ID and random numbers, but not the summation. 我的猜测是因为 Sum 属性是在 MyItem 绑定到 MyItemControl 之前计算的。我尝试在设置 MyItem 属性时提高属性更改,但这似乎也不起作用。


在 xaml 中绑定 MyItem 属性后,如何更新 Sum 属性?


我的项目类:


public class MyItem

{

    private static Random random = new Random();


    public MyItem(string ID)

    {

        this.ID = ID;

        Num1 = random.Next();

        Num2 = random.Next();

        Num3 = random.Next();

    }


    public string ID { get; private set; }


    public int Num1 { get; private set; }


    public int Num2 { get; private set; }


    public int Num3 { get; private set; }


    public override string ToString()

    {

        return ID;

    }

}

MyItemControl xaml:


<UserControl x:Name="userControl" x:Class="testApp2.MyItemControl"

             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 

             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 

             xmlns:local="clr-namespace:testApp2"

             mc:Ignorable="d" 

             d:DesignHeight="300" d:DesignWidth="300">

    <StackPanel Margin="0">

        <StackPanel Margin="0" Orientation="Horizontal">

            <Label Content="ID:" Margin="0" HorizontalAlignment="Left"/>

            <Label Content="{Binding MyItem.ID, ElementName=userControl, Mode=OneWay}" Margin="0" HorizontalAlignment="Left"/>


繁花如伊
浏览 153回答 1
1回答

桃花长相依

来自克莱门斯的建议。我删除了 INotifyPropertyChanged 接口,并将 PropertyChangedCallback 与重新计算 Sum 属性的 MyItem 属性一起使用。这似乎工作正常。更新了 MyItemControl:public partial class MyItemControl : UserControl{&nbsp; &nbsp; public MyItemControl()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; InitializeComponent();&nbsp; &nbsp; &nbsp; &nbsp; Sum = MyItem != null ? MyItem.Num1 + MyItem.Num2 + MyItem.Num3 : 0;&nbsp; &nbsp; }&nbsp; &nbsp; public MyItem MyItem&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get { return (MyItem)GetValue(MyItemProperty); }&nbsp; &nbsp; &nbsp; &nbsp; set { SetValue(MyItemProperty, value); }&nbsp; &nbsp; }&nbsp; &nbsp; public static readonly DependencyProperty MyItemProperty =&nbsp; &nbsp; &nbsp; &nbsp; DependencyProperty.Register("MyItem", typeof(MyItem), typeof(MyItemControl), new PropertyMetadata(null, MyItemPropertyChanged));&nbsp; &nbsp; private static void MyItemPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; MyItemControl sender = (MyItemControl)obj;&nbsp; &nbsp; &nbsp; &nbsp; sender.Sum = sender.MyItem.Num1 + sender.MyItem.Num2 + sender.MyItem.Num3;&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Update sum");&nbsp; &nbsp; }&nbsp; &nbsp; public int Sum&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get { return (int)GetValue(SumProperty); }&nbsp; &nbsp; &nbsp; &nbsp; set { SetValue(SumProperty, value); }&nbsp; &nbsp; }&nbsp; &nbsp; private static readonly DependencyProperty SumProperty =&nbsp; &nbsp; &nbsp; &nbsp; DependencyProperty.Register("Sum", typeof(int), typeof(MyItemControl), new PropertyMetadata(0));}
随时随地看视频慕课网APP
我要回答