Code中的绑定不会对源对象更改做出反应

时间:2022-07-17 19:41:55

I have a dependency property on a class inheriting from adorner, like so:

我在继承自adorner的类上有依赖属性,如下所示:

public class LoadingAdorner : Adorner
{
    public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof (string), typeof (LoadingAdorner), new PropertyMetadata(default(string)));

    public string Text
    {
        get { return (string) GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    public static readonly DependencyProperty IsShowingProperty = DependencyProperty.Register("IsShowing", typeof (bool), typeof (LoadingAdorner), new PropertyMetadata(default(bool)));

    ...
}

Adorner's don't really have any XAML, but I wanted the text of this adorner to be bindable to the viewmodel. So I create the binding in code, in the view's constructor, like so:

Adorner并没有真正的XAML,但我希望这个adorner的文本可以绑定到viewmodel。所以我在视图的构造函数中创建了代码中的绑定,如下所示:

    private readonly LoadingAdorner _loading;

    public MainWindow()
    {
        InitializeComponent();
        _loading = new LoadingAdorner(MainPage);
        var bind = new Binding("LoadingText"){Source = DataContext};
        _loading.SetBinding(LoadingAdorner.TextProperty, bind);
    }

The DataContext is my view model, my view model implements INotifyPropertyChanged, LoadingText is a string property that calls OnPropertyChanged, etc. All bindings in XAML work fine, however, the code binding does not.

DataContext是我的视图模型,我的视图模型实现了INotifyPropertyChanged,LoadingText是一个调用OnPropertyChanged等的字符串属性.XAML中的所有绑定都可以正常工作,但是代码绑定没有。

I believe it is because at the time of creating the binding, the view model has not yet been set to the DataContext (it is null), I do this on the line after creating the view. If I set this binding to a property on my view using Source = this, it works.

我相信这是因为在创建绑定时,视图模型尚未设置为DataContext(它为null),我在创建视图后在行上执行此操作。如果我使用Source = this将此绑定设置为我的视图上的属性,则它可以正常工作。

My question is, why are the XAML bindings are capable of reacting to the source object changing, while the code binding doesn't appear to be? Is there a proper way for me to create a binding that will react to this similiar to the XAML bindings?

我的问题是,为什么XAML绑定能够对源对象的变化作出反应,而代码绑定似乎不是?有没有一种正确的方法让我创建一个对XAML绑定类似的响应?

1 个解决方案

#1


0  

Binding do not and cannot react to source changes, it is a logical impossibility, objects do not change properties and references to objects change. Bindings can react to DataContext property changes but only if you do not do something horrible like Source = DataContext which kills the mechanism by getting the current data context once only. Just drop that so the DataContext is the default source again and the binding should react to the changes.

绑定不会也不能对源更改做出反应,这是逻辑上的不可能性,对象不会更改属性并且对对象的引用会发生变化。绑定可以对DataContext属性更改做出反应,但前提是您没有像Source = DataContext那样做一些可怕的事情,它只通过获取当前数据上下文一次来杀死机制。只需删除它,以便DataContext再次成为默认源,绑定应该对更改做出反应。

If the DataContext is on another object than the one that is bound it needs to be moved into the Path, i.e. new Binding("DataContext.LoadingText"){ Source = this }.

如果DataContext位于另一个对象而不是绑定的对象上,则需要将其移动到Path中,即new Binding(“DataContext.LoadingText”){Source = this}。

#1


0  

Binding do not and cannot react to source changes, it is a logical impossibility, objects do not change properties and references to objects change. Bindings can react to DataContext property changes but only if you do not do something horrible like Source = DataContext which kills the mechanism by getting the current data context once only. Just drop that so the DataContext is the default source again and the binding should react to the changes.

绑定不会也不能对源更改做出反应,这是逻辑上的不可能性,对象不会更改属性并且对对象的引用会发生变化。绑定可以对DataContext属性更改做出反应,但前提是您没有像Source = DataContext那样做一些可怕的事情,它只通过获取当前数据上下文一次来杀死机制。只需删除它,以便DataContext再次成为默认源,绑定应该对更改做出反应。

If the DataContext is on another object than the one that is bound it needs to be moved into the Path, i.e. new Binding("DataContext.LoadingText"){ Source = this }.

如果DataContext位于另一个对象而不是绑定的对象上,则需要将其移动到Path中,即new Binding(“DataContext.LoadingText”){Source = this}。