Justin Pihony
Justin Pihony

Reputation: 67085

How to programmatically change binding when datacontext changes

I have a custom control that allows the consumer to send in markup that will be parsed into Inlines, and it will try to match a given Command name with its appropriate ICommand. I have this working except in the case where the DataContext is not set yet. I know that storing the markup and reloading it upon DataContextChanged is not appropriate, but I cannot seem to find anything that works yet. I have tried BindingExpression and Binding to no avail as I do not see a way to attach them to a Hyperlink

Upvotes: 2

Views: 285

Answers (2)

Justin Pihony
Justin Pihony

Reputation: 67085

Looking into some other code, I realized that this is all I needed to do:

hyperlink.SetBinding(Hyperlink.CommandProperty, new Binding(description.Command));

The WPF code picks it up generically...I will need to reflect to see exactly what it does, but it works. I could not set the Command property directly, but this worked :)

Upvotes: 0

Reed Copsey
Reed Copsey

Reputation: 564481

I know that storing the markup and reloading it upon DataContextChanged is not appropriate

If you're going to be building a binding to ICommand instances within the DataContext, this is likely the only option that will make sense.

Otherwise, you'd never be able to correct the binding when the data context is changed.

Upvotes: 1

Related Questions