如何发现引用类型属性在视图模型中发生了变化

本文关键字:视图 模型 变化 发生了 属性 引用类型 何发现 发现 | 更新日期: 2023-09-27 18:18:07

我有一个这样的UserControl:

 <Button Grid.Row="1" X:Name="ChangeButton" Click="ChangeButton_Click">Change</Button>

My UserControl CodeBehind:

    public static readonly DependencyProperty SelectedPersonProperty =
 DependencyProperty.Register("SelectedPerson", typeof(PersonModel), typeof(MyControl),
     new FrameworkPropertyMetadata
     {
         DefaultValue=null,
         BindsTwoWayByDefault = true
     });
    public PersonModel SelectedPerson
    {
        get { return (PersonModel)GetValue(SelectedPersonProperty); }
        set { SetValue(SelectedPersonProperty, value);}
    }
  public static readonly DependencyProperty TextValueProperty =
    DependencyProperty.Register("TextValue", typeof(string), typeof(NumericTextBox),
        new  FrameworkPropertyMetadata{
                BindsTwoWayByDefault=true
                });
    public string TextValue
    {
        get { return (string)GetValue(TextValueProperty); }
        set { 
            SetValue(TextValueProperty, value);
        }
    }
public MyControl(){
        SelectedPerson = new PersonModel();
}
 private void ChangeButton_Click(object sender, RoutedEventArgs e)
    {
        TextValue="Hello";
        SelectedPerson.Age = 12;
        SelectedPerson.FirstName = "AA";
        SelectedPerson.LastName = "BB";
    }

在我的MainView Xaml代码我有:

<UC:MyControl SelectedPerson="{Binding Person}" TextValue="{Binding Path=Name,UpdateSourceTrigger=PropertyChanged}" ></UC:MyControl>
  <TextBox Grid.Row="1"  Background="Yellow" Text="{Binding Path=Person.Name,UpdateSourceTrigger=PropertyChanged}"  />

MainViewModel代码:

     PersonModel person
     public PersonModel Person { 
            get { return _person; }
            set
            {
                if (_person!= value)//i set a breakpoint here
                {
                    _person= value;
                    RaisePropertyChanged("Person ");
                }
            }
 }
   string _name;
    public string Name
    {
        get { return _name; }
        set
        {
            if (_name != value)//i set a breakpoint here.when TextValue changed this breakepoint actived
            {
            _name = value;
            RaisePropertyChanged("Name");
            }
        }
    }

当我点击ChangeButton在我的用户控件的selectedperson在usercontrol改变和我的文本框在我的视图显示名称像"AA"。但我的断点在视图模式中没有激活。我怎么能发现,当我的人在视图模式改变?

如何发现引用类型属性在视图模型中发生了变化

你需要在PersonModel内部实现INotifyPropertyChanged,并在你要改变的属性上设置一个断点,而不是整个Person对象属性。就像

class PersonModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };
    string name;
    public string Name
    {
        get { return name; }
        set
        {
            name = value; <---- set breakpoint here
            PropertyChanged(this, new PropertyChangedEventArgs("Name"));
        }
    }
}

然后你可以像这样监听personmodel类的变化:

myParentObject.Person.PropertyChanged += (sender,e)=>
{
    //do something here
}

要达到Person setter的断点,您需要更新ChangeButton_Click如下:

private void ChangeButton_Click(object sender, RoutedEventArgs e)
    {
        TextValue="Hello";
        PersonModel newPerson = new PersonModel();
        newPerson.Age = 12;
        newPerson.FirstName = "AA";
        newPerson.LastName = "BB";
        SelectedPerson = newPerson;
    }

你所做的只是修改现有PersonModel对象的属性。这样你就不会在PersonModel对象setter上遇到断点,但是你会在PersonModel类的属性setter上遇到断点。通过像上面那样修改代码,您将遇到断点。