简体   繁体   English

数据绑定/ PropertyChanged为空

[英]Data Binding / PropertyChanged is null

Ok, I'm lost. 好吧,我迷路了。 I'm searching since yesterday why my databinding doesn't work as expected. 自昨天以来,我一直在搜索为什么我的数据绑定无法按预期进行的原因。 As I'm new to WPF and also to MVVM it would be understandable if I would not use the same approach on the same project with 2 other views. 因为我是WPF和MVVM的新手,所以如果我不对其他2个视图在同一项目上使用相同的方法,那将是可以理解的。

The essential configuration is that I have 3 views, for all of them I set the DataContext in the MainWindows-Constructor to the responsible ViewModel. 基本配置是我有3个视图,对于所有这些视图,我都将MainWindows-Constructor中的DataContext设置为负责的ViewModel。

The view model itself does not implement IPropertyChanged but the model has a few nested objects which do. 视图模型本身不实现IPropertyChanged,但是该模型具有一些嵌套的对象。

I bind with the following XAML: 我绑定以下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>

InsertLine has PropertyChanged: InsertLine具有PropertyChanged:

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

The general binding to Destination and Unit works. 与目标和单元的一般绑定有效。 As does the validation. 验证也一样。

However I do at some point in time set a new insertline, after it has been inserted into an ObeservableCollection (_document.Lines). 但是,在将其插入到ObeservableCollection(_document.Lines)中之后,我会在某个时候设置新的插入行。 What I expect to happen (and what does happen in the 2 other views) is that the textbox get's cleared. 我期望发生的事情(以及其他2个视图中发生的事情)是清除了文本框。

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

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

        InsertLine = newline;
    }

The property on InsertLine get's called, however the PropertyChanged delegate is null. 调用InsertLine上的属性,但是PropertyChanged委托为null。

Does anybody have an idea why it's null? 有人知道为什么它为空吗? The same approach works with 2 separate views, but not with this one... 相同的方法适用于2个独立的视图,但不适用于此视图。

Or do I have a wrong idea and there is a better way to do it? 还是我有一个错误的主意,并且有更好的方法呢?

Ok, I found it. 好的,我找到了。 And it was an absolutely stupid oversight. 这绝对是愚蠢的疏忽。

For anybody else who has this problem: Make sure that you not only have the PropertyChanged event and you are calling it, you should also inherit from the interface. 对于任何其他有此问题的人:确保您不仅具有PropertyChanged事件并正在调用它,还应该从该接口继承。 I simply forgot to put INotifyPropertyChanged after the model's definition. 我只是忘了将INotifyPropertyChanged放在模型的定义之后。

Which brings me to a question out of curiosity: I always thought that an interface was nothing more than a contract, essentially it should not make any difference, but I assume that WPF uses reflection/is-operator to find out if an object can be bound to. 出于好奇,这使我想到一个问题:我一直以为接口只不过是一个契约,从本质上讲它不应该有任何区别,但是我假设WPF使用反射/操作符来确定对象是否可以是对象。势必。 Am I roughly on the right track there? 我在那里大致正确吗?

Just in case: I had a similar problem but my mistake was that class which implemented INotifyPropertyChanged was private. 以防万一:我有一个类似的问题,但是我的错误是实现INotifyPropertyChanged的类是私有的。 Making it public resolved my case. 公开解决了我的案件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM