何时执行RaisePropertyChanged?

cur*_*ity 2 c# wpf

我无法在View Model的SET属性中定位断​​点,因此默认值未更改.(获取 - 没问题,它使用有效的默认值初始化我的文本框.)

我有一个model.cs,其中定义了一个公共字符串字段

model.cs
{
..
public textDefValue = "aaa";
}
Run Code Online (Sandbox Code Playgroud)

这是一个ViewModel

{
.. 
 Model model = new Model();
....
 public string TextField
        {
            get { return model.textDefValue; }
            set
            {
               //break point here
                model.textDefValue = value;
                RaisePropertyChanged(TextField);
            }
        }
 ....
   protected void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
}
Run Code Online (Sandbox Code Playgroud)

和XAML:

<TextBox x:Name="myBox" Text="{Binding ViewModel.TextField, Mode=TwoWay}">
Run Code Online (Sandbox Code Playgroud)

我想当我在这个文本框中输入一些东西时,SET会起作用,我将针对一个断点但是,我无法在SET中击中这个中断.哪个错误?

Dan*_*zey 6

没有错误,只是一个误解.

默认情况下,控件.Text属性的绑定仅在您离开框时更新(即将焦点移动到其他控件).您需要单击或标签离开要更新的值和要命中的断点.

您可以通过更新绑定来更改此行为,如下所示:

Text="{Binding ViewModel.TextField, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Run Code Online (Sandbox Code Playgroud)

这将导致绑定在每次文本值更改时更新 - 即文本框中的按键.