R – Get DynamicResource Binding in WPF

bindingdynamicresourcewpf

Can any one help me to get DynamicResource Binding in WPF by code?

I have set binding Like follow,

TextBlock Background={DynamicResource ColorA} Name="TB" in Xaml.

and i need to get the – TB's background is binded to "ColorA".
how can i access this DynamicResource Binding Expression in WPF by coding.
when i try to get TB.Background, it is white(#FFFFF..) or if i already given the value to the
Resorce key "ColorA" that will be given.
but i want to get this Binding Expression.
Thank in advance for your Help.


I think my Question wasn't clear.
I want to get What Reource Binding was done to the "TB" in Xaml by code.
But the aren't any TB.GetResourceReference. I Want some think like that.
Where that Binding expression is kept in WPF. I need to get the TB's BackgroundProperty was
Binded to Which( answer "ColorA") key?
thank a lot for sudden response.

Best Answer

You can use the FrameworkElement.SetResourceReference method:

MSDN: http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.setresourcereference.aspx

Provided your xaml has this:

<TextBlock x:Name="TB">

You can write this in the code behind:

TB.SetResourceReference(BackgroundProperty, "ColorA");