Why target update before source ? A strange problem when using binding.A data class MyTestClass has a property IsTestChecked. Have a toggle in the data template of this class and binding the IsChecked property of the toggle button to IsTestChecked.<br><br>&lt;ToggleButton<br>...<br>IsChecked=&quot;{Binding IsTestChecked, Mode=TwoWay}&quot;<br>...<br>/&gt; <p>I find a strange thing on this binding, that when i clicke at the toggle button, the IsTestChecked property of MyTestClass </p> <p>changes before the IsChecked property of ToggleButton. In my understanding, the ToggleButton.IsChecked should be changed </p> <p>firstly and then change the MyTestClass.IsTestChecked because of the binding. I am confused about this issue.<br><br>Can anyone give me some suggestion? <br><br>Thank you very much!</p>© 2009 Microsoft Corporation. All rights reserved.Thu, 19 Jun 2008 00:15:52 Zfb63b572-4b2c-41cd-bc46-4230ef07ac4ehttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/fb63b572-4b2c-41cd-bc46-4230ef07ac4e#fb63b572-4b2c-41cd-bc46-4230ef07ac4ehttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/fb63b572-4b2c-41cd-bc46-4230ef07ac4e#fb63b572-4b2c-41cd-bc46-4230ef07ac4eClarkZenghttp://social.msdn.microsoft.com/Profile/en-US/?user=ClarkZengWhy target update before source ? A strange problem when using binding.A data class MyTestClass has a property IsTestChecked. Have a toggle in the data template of this class and binding the IsChecked property of the toggle button to IsTestChecked.<br><br>&lt;ToggleButton<br>...<br>IsChecked=&quot;{Binding IsTestChecked, Mode=TwoWay}&quot;<br>...<br>/&gt; <p>I find a strange thing on this binding, that when i clicke at the toggle button, the IsTestChecked property of MyTestClass </p> <p>changes before the IsChecked property of ToggleButton. In my understanding, the ToggleButton.IsChecked should be changed </p> <p>firstly and then change the MyTestClass.IsTestChecked because of the binding. I am confused about this issue.<br><br>Can anyone give me some suggestion? <br><br>Thank you very much!</p>Thu, 05 Jun 2008 12:35:08 Z2008-06-05T12:35:08Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/fb63b572-4b2c-41cd-bc46-4230ef07ac4e#e0389ff0-babb-4326-aa61-b42fb445cee1http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/fb63b572-4b2c-41cd-bc46-4230ef07ac4e#e0389ff0-babb-4326-aa61-b42fb445cee1ClarkZenghttp://social.msdn.microsoft.com/Profile/en-US/?user=ClarkZengWhy target update before source ? A strange problem when using binding. Can anyone give me some suggestion? <br><br><br>ThanksFri, 06 Jun 2008 01:17:30 Z2008-06-06T01:17:30Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/fb63b572-4b2c-41cd-bc46-4230ef07ac4e#82fafc1b-e944-483f-9988-15ed110c18c7http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/fb63b572-4b2c-41cd-bc46-4230ef07ac4e#82fafc1b-e944-483f-9988-15ed110c18c7Wodahshttp://social.msdn.microsoft.com/Profile/en-US/?user=WodahsWhy target update before source ? A strange problem when using binding. Why is that causing a problem?<hr size="1" align="left" width="25%">John FentonFri, 06 Jun 2008 02:10:28 Z2008-06-06T02:10:28Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/fb63b572-4b2c-41cd-bc46-4230ef07ac4e#d7956816-94a4-4092-bea3-ec39bc11b930http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/fb63b572-4b2c-41cd-bc46-4230ef07ac4e#d7956816-94a4-4092-bea3-ec39bc11b930Atul Guptahttp://social.msdn.microsoft.com/Profile/en-US/?user=Atul%20GuptaWhy target update before source ? A strange problem when using binding.<div class=quote><font class=quoteHeader>ClarkZeng said:</font> <p> Can anyone give me some suggestion? <br><br><br>Thanks</p></div><br><br>Is there any problem due to this behavior? I guess since ToggleButton has a property change callback set for the IsCheckedProperty, it fires before the value is updated and this causes the binding to update before you can see the value of the property update itself<hr size="1" align="left" width="25%">MVP Client AppFri, 06 Jun 2008 03:57:14 Z2008-06-06T03:57:14Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/fb63b572-4b2c-41cd-bc46-4230ef07ac4e#5cf86756-9017-4496-96b6-ec489b40a1c2http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/fb63b572-4b2c-41cd-bc46-4230ef07ac4e#5cf86756-9017-4496-96b6-ec489b40a1c2Atul Guptahttp://social.msdn.microsoft.com/Profile/en-US/?user=Atul%20GuptaWhy target update before source ? A strange problem when using binding. I just verified the behavior by writing my own dependency property as something like below<br><br> <div style="font-size:8pt;background:white;color:black;font-family:Lucida Sans Unicode"> <p style="margin:0px">        <span style="color:red">public</span> <span style="color:red">static</span> <span style="color:red">readonly</span> <span style="color:#2b91af">DependencyProperty</span> IsButtonCheckedProperty =</p> <p style="margin:0px">            <span style="color:#2b91af">DependencyProperty</span>.Register(<span style="color:#a31515">&quot;IsChecked&quot;</span>, <span style="color:red">typeof</span>(<span style="color:red">bool</span>), <span style="color:red">typeof</span>(<span style="color:#2b91af">ComboTest</span>), </p> <p style="margin:0px">            <span style="color:red">new</span> <span style="color:#2b91af">FrameworkPropertyMetadata</span>(<span style="color:red">false</span>, </p> <p style="margin:0px">                <span style="color:#2b91af">FrameworkPropertyMetadataOptions</span>.Journal | <span style="color:#2b91af">FrameworkPropertyMetadataOptions</span>.BindsTwoWayByDefault, </p> <p style="margin:0px">                <span style="color:red">new</span> <span style="color:#2b91af">PropertyChangedCallback</span>(OnIsCheckedChanged)));</p> <p style="margin:0px"> </p> <p style="margin:0px">        <span style="color:red">private</span> <span style="color:red">static</span> <span style="color:red">void</span> OnIsCheckedChanged(<span style="color:#2b91af">DependencyObject</span> d, <span style="color:#2b91af">DependencyPropertyChangedEventArgs</span> e)</p> <p style="margin:0px">        {</p> <p style="margin:0px">            <span style="color:green">//this is just a dummy code to see when this method is called</span></p> <p style="margin:0px">            <span style="color:red">string</span> str = <span style="color:#a31515">&quot;hello world&quot;</span>;</p> <p style="margin:0px">        }</p> <p style="margin:0px"> </p> <p style="margin:0px">        <span style="color:red">public</span> <span style="color:red">bool</span> IsButtonChecked </p> <p style="margin:0px">        {</p> <p style="margin:0px">            <span style="color:red">get</span> { <span style="color:red">return</span> (<span style="color:red">bool</span>)GetValue(IsButtonCheckedProperty); }</p> <p style="margin:0px">            <span style="color:red">set</span> { SetValue(IsButtonCheckedProperty, <span style="color:red">value</span>); }</p> <p style="margin:0px">        }</p></div><br><br>I put break points in both the SetValue call and also the OnIsCheckedChanged method. On execution, I found that the breakpoint on SetValue is hit first, but before it is completed, it goes to the OnIsCheckedChanged method, finishes executing it and then comes back to SetValue method. <br><br>If we take the ToggleButton example, it also has similar behavior. Hence before the value of IsChecked DP is updated, the call to OnIsCheckedChanged causes the event to fire, and this will cause the binding to update the value. Finally after this is done, the control comes back to SetValue call and the property assignment is completed. <br><br>Hope this helps !  <hr size="1" align="left" width="25%">MVP Client AppFri, 06 Jun 2008 04:10:19 Z2008-06-06T04:10:19Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/fb63b572-4b2c-41cd-bc46-4230ef07ac4e#8a74b195-0c77-4833-9655-7636cca90979http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/fb63b572-4b2c-41cd-bc46-4230ef07ac4e#8a74b195-0c77-4833-9655-7636cca90979Wodahshttp://social.msdn.microsoft.com/Profile/en-US/?user=WodahsWhy target update before source ? A strange problem when using binding. Isn't that by design.<br><br><font color="#2b91af" size=2><font color="#2b91af" size=2> <p>DependencyPropertyChangedEventArgs</font></font><font size=2> e<br></font><font size=2>e.NewValue &lt;-- contains the value you should be checking.<br><br>Still not sure why this would be a problem?<br><br></p></font> <hr align=left width="25%" size=1> John Fenton<br><br>Nice demo of how it works BTW!Fri, 06 Jun 2008 04:37:10 Z2008-06-06T04:56:41Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/fb63b572-4b2c-41cd-bc46-4230ef07ac4e#1ddc8d2a-98cf-42b3-9c49-fff2ce501287http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/fb63b572-4b2c-41cd-bc46-4230ef07ac4e#1ddc8d2a-98cf-42b3-9c49-fff2ce501287ClarkZenghttp://social.msdn.microsoft.com/Profile/en-US/?user=ClarkZengWhy target update before source ? A strange problem when using binding.<div class=quote><font class=quoteHeader>Atul Gupta said:</font> <p>I just verified the behavior by writing my own dependency property as something like below<br><br> <div style="font-size:8pt;background:white;color:black;font-family:Lucida Sans Unicode"> <p style="margin:0px">        <font color=red>public</font> <font color=red>static</font> <font color=red>readonly</font> <font color="#2b91af">DependencyProperty</font> IsButtonCheckedProperty =</p> <p style="margin:0px">            <font color="#2b91af">DependencyProperty</font>.Register(<font color="#a31515">&quot;IsChecked&quot;</font>, <font color=red>typeof</font>(<font color=red>bool</font>), <font color=red>typeof</font>(<font color="#2b91af">ComboTest</font>), </p> <p style="margin:0px">            <font color=red>new</font> <font color="#2b91af">FrameworkPropertyMetadata</font>(<font color=red>false</font>, </p> <p style="margin:0px">                <font color="#2b91af">FrameworkPropertyMetadataOptions</font>.Journal | <font color="#2b91af">FrameworkPropertyMetadataOptions</font>.BindsTwoWayByDefault, </p> <p style="margin:0px">                <font color=red>new</font> <font color="#2b91af">PropertyChangedCallback</font>(OnIsCheckedChanged)));</p> <p style="margin:0px"> </p> <p style="margin:0px">        <font color=red>private</font> <font color=red>static</font> <font color=red>void</font> OnIsCheckedChanged(<font color="#2b91af">DependencyObject</font> d, <font color="#2b91af">DependencyPropertyChangedEventArgs</font> e)</p> <p style="margin:0px">        {</p> <p style="margin:0px">            <font color=green>//this is just a dummy code to see when this method is called</font></p> <p style="margin:0px">            <font color=red>string</font> str = <font color="#a31515">&quot;hello world&quot;</font>;</p> <p style="margin:0px">        }</p> <p style="margin:0px"> </p> <p style="margin:0px">        <font color=red>public</font> <font color=red>bool</font> IsButtonChecked </p> <p style="margin:0px">        {</p> <p style="margin:0px">            <font color=red>get</font> { <font color=red>return</font> (<font color=red>bool</font>)GetValue(IsButtonCheckedProperty); }</p> <p style="margin:0px">            <font color=red>set</font> { SetValue(IsButtonCheckedProperty, <font color=red>value</font>); }</p> <p style="margin:0px">        }</p></div><br><br>I put break points in both the SetValue call and also the OnIsCheckedChanged method. On execution, I found that the breakpoint on SetValue is hit first, but before it is completed, it goes to the OnIsCheckedChanged method, finishes executing it and then comes back to SetValue method. <br><br>If we take the ToggleButton example, it also has similar behavior. Hence before the value of IsChecked DP is updated, the call to OnIsCheckedChanged causes the event to fire, and this will cause the binding to update the value. Finally after this is done, the control comes back to SetValue call and the property assignment is completed. <br><br>Hope this helps !   <hr align=left width="25%" size=1> MVP Client App <p></p></div><br><br> Thank you very much!<br><br>Actually, i want to control my IsChecked property with a IsCheckable flag in UI (just like MenuItem), not in data class.  That is because that i don't want user can change IsChecked by Click or Enter, but developer can change it by using the property of data class.<br>Sat, 07 Jun 2008 01:46:19 Z2008-06-07T01:46:19Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/fb63b572-4b2c-41cd-bc46-4230ef07ac4e#1322d9ae-afb7-4700-b746-8b5edbdbaa49http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/fb63b572-4b2c-41cd-bc46-4230ef07ac4e#1322d9ae-afb7-4700-b746-8b5edbdbaa49Wodahshttp://social.msdn.microsoft.com/Profile/en-US/?user=WodahsWhy target update before source ? A strange problem when using binding.For read only on a checkbox, you just set IsEnabled=&quot;false&quot; you probably will also want to override the template to make it display the way you want.<br><br><hr size="1" align="left" width="25%">John FentonSat, 07 Jun 2008 03:37:05 Z2008-06-07T03:37:05Z