Monday, September 10, 2012

Get and set TextBlock.Text binding converter in code behind

Get and set TextBlock.Text binding converter in code behind

I have a binding set to the Text property of a TextBlock in my XAML this way:

<TextBlock x:Name="MyTextBlock" TextWrapping="Wrap" Text="{Binding TextProperty, Converter={StaticResource MyConverter}}"/> 

I want to change the converter from the code-behind dependind on which is currently used. How can I get and set the converter of the binding from code behind? Id' like somthing like:

if (converter = x)     converter = y; else     converter = x; 

Answers & Comments...

Answer: 1

You'll need to get the binding itself:

var binding = BindingOperations.GetBindingBase(     MyTextBlock,     TextBlock.TextProperty); if (binding != null) {     // 3 types of bindings, choose the right one     if (binding is Binding)     {         ((Binding)binding).Converter = yourLogicHere;     }     else if (binding is MultiBinding)     {         ((MultiBinding)binding).Converter = yourMultiLogicHere;     }     else if (binding is PriorityBinding)     {         foreach (var childBinding in ((PriorityBinding)binding).Bindings)         {             ((Binding)childBinding).Converter = yourLogicHere;         }     } } 
by : sixlettervariableshttp://stackoverflow.com/users/7116




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog