Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cases when binding are detached automatically

Today I faced a problem which reminded me of bindings being detached automatically in some cases. I am not sure but here is the scenario -

I attach a menu item bound to a property(implementing INotifyPropertyChanged), like this -

IsChecked="{Binding Path=DisplayLongUnit, Mode=TwoWay}"

Now in its checked event handler I update its IsChecked value after checking some condition like this -

If( condition == true){menuItem.IsChecked = true}

Now will the binding be still attached or it will get lost? (I remember reading somewhere that it will be lost).

Are there any scenarios in which bindings will be detached automatically?

One I could find out is mentioned here -

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/08d6e4c4-47ab-44f3-b19a-c0ab872fb1a8

like image 859
akjoshi Avatar asked Jul 10 '26 19:07

akjoshi


1 Answers

About knowing whether a binding has been detached: what you can do is debug your binding. First you add the diagnostics namespace in your XAML. Here's an example on a Window (I've removed the other usual namespaces for clarity):

<Window xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase">  

then you enable trace on a binding. Here's an example with a binding set on a TextBox's Text property:

<TextBox Text={Binding FirstName, diagnostics:PresentationTraceSources.TraceLevel=High} />  

Now if you look in the Output tab of Visual Studio when running your program, you'll see informations about your binding, including "detach" when it's been detached.

So this doesn't tell you WHY it was detached, only when. It can help though.

like image 158
pequatre Avatar answered Jul 13 '26 15:07

pequatre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!