如何找出viewmodel中的引用类型属性已更改?

时间:2011-12-18 14:24:43

标签: c# wpf mvvm

我有一个像这样的UserControl:

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

我的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时,usercontrol中的selectedperson已更改,我视图中的Textbox显示名称为“AA”。但我在viewmode中的breake点未激活。如果我的Person in viewmode改变了吗?

2 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:1)

要为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上命中断点。通过修改上面的代码,你会遇到断点。