简体   繁体   中英

WPF Dynamic Nested Binding

Suppose I have a view with the following control (DataContext is properly set to a view model that implements INotify):

When the view is first shown, Document is non existant (null). During runtime (after the user opens a Document), then Document and dependent structure (including Document.SelectedFrame.Image) is created.

At that point, I do invoke the PropertyChaned handler of my SelectedFrame object (which also implements INotifyProperty), but nothing happens.

Do I have to re-tie the bindings at runtime when Document gets created?

I assume you have a binding which looks something like this:

<Image Source="{Binding Path=Document.SelectedFrame.Image}"></Image>

You need to raise PropertyChanged on the ViewModel class when the value of Document changes. It should look something like this:

public object Document
{
    get { return document; }
    set
    {
        document = value;
        this.PropertyChanged(this, new PropertyChangedEventArgs("Document"));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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