Data Binding/PropertyChanged为null

Fin*_*riX 1 c# data-binding wpf

好的,我迷路了.我从昨天开始搜索为什么我的数据绑定不能按预期工作.由于我是WPF和MVVM的新手,如果我不在同一项目中使用相同的方法和其他2个视图,那将是可以理解的.

基本配置是我有3个视图,对于所有这些视图,我将MainWindows-Constructor中的DataContext设置为负责的ViewModel.

视图模型本身不实现IPropertyChanged,但模型有一些嵌套对象.

我绑定以下XAML:

<TextBox Grid.Column="1" Grid.Row="0" Margin="1">
                <TextBox.Text>
                    <Binding Path="Model.InsertLine.Destination.Value" UpdateSourceTrigger="PropertyChanged">
                        <Binding.ValidationRules>
                            <local:MinMaxLengthValidatonRule Min="7" Max="7"/>
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
            </TextBox>
Run Code Online (Sandbox Code Playgroud)

InsertLine具有PropertyChanged:

    public TLTMoveULLine InsertLine
    {
        get { return _insertline; }
        internal set
        {
            _insertline = value;
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("InsertLine"));
        }
    }
Run Code Online (Sandbox Code Playgroud)

对目标和单元的常规绑定有效.和验证一样.

但是我在某个时间点设置了一个新的插入行,在它插入ObeservableCollection(_document.Lines)之后.我期望发生的事情(以及其他2个视图中发生的事情)是文本框被清除.

    public void AddLine()
    {
        _document.Lines.Add(InsertLine);
        TLTMoveULLine newline = new TLTMoveULLine();

        newline.Destination.Value = InsertLine.Destination.Value;
        newline.Unit.Value = "";

        InsertLine = newline;
    }
Run Code Online (Sandbox Code Playgroud)

InsertLine上的属性被调用,但PropertyChanged委托为null.

有没有人知道为什么它是空的?相同的方法适用于2个单独的视图,但不适用于这个...

或者我有错误的想法,还有更好的方法吗?

Fin*_*riX 7

好的,我找到了.这绝对是一个愚蠢的疏忽.

对于遇到此问题的任何其他人:确保您不仅拥有PropertyChanged事件并且您正在调用它,您还应该继承该接口.我只是忘了在模型定义之后放入INotifyPropertyChanged.

这带给我一个好奇的问题:我一直认为接口只不过是一个契约,本质上它应该没有任何区别,但我认为WPF使用reflection/is-operator来查明对象是否可以势必.我在那里大致走在正确的轨道上吗?