c# - 如何确定 View 模型中的引用类型属性已更改?

标签 c# wpf mvvm

我有一个像这样的UserControl:

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

我的UserControl代码背后:
    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时,用户控件中的所选人员已更改,并且我的文本框在 View 中显示为“AA”之类的名称。但是我的断点在viewmode中未激活。如何更改Viewmode中的Person?

最佳答案

您需要在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
}

关于c# - 如何确定 View 模型中的引用类型属性已更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8552348/

相关文章:

c# - Visual Studio Lightswitch Beta2

c# - 应用栏窗口从停靠位置弹出,然后移动到停靠位置

c# - WPF ListBoxItem MVVM 中的 SelectedItem 问题

wpf - 当 View 模型在构造函数中有参数时,如何将 View 模型连接到 View ?

C# - 正则表达式验证日期和小时

c# - 何时在数据类中使用 char 字段

c# - 在继承类上多次定义接口(interface)

wpf - 将可见性绑定(bind)到 Text.Length

wpf - 在模型或 View 模型上实现INotifyPropertyChanged?

mvvm - 在 Windows 10 通用应用程序中将命令绑定(bind)到集线器部分标题单击