Microsoft Developer Network > Forenhomepage > Windows Presentation Foundation (WPF) > Why target update before source ? A strange problem when using binding.
Stellen Sie eine FrageStellen Sie eine Frage
 

BeantwortetWhy target update before source ? A strange problem when using binding.

  • Donnerstag, 5. Juni 2008 12:35ClarkZeng TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    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.

    <ToggleButton
    ...
    IsChecked="{Binding IsTestChecked, Mode=TwoWay}"
    ...
    />

    I find a strange thing on this binding, that when i clicke at the toggle button, the IsTestChecked property of MyTestClass

    changes before the IsChecked property of ToggleButton. In my understanding, the ToggleButton.IsChecked should be changed

    firstly and then change the MyTestClass.IsTestChecked because of the binding. I am confused about this issue.

    Can anyone give me some suggestion?

    Thank you very much!

Antworten

  • Freitag, 6. Juni 2008 04:10Atul GuptaMVPTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     BeantwortetEnthält Code
    I just verified the behavior by writing my own dependency property as something like below

            public static readonly DependencyProperty IsButtonCheckedProperty =

                DependencyProperty.Register("IsChecked", typeof(bool), typeof(ComboTest),

                new FrameworkPropertyMetadata(false,

                    FrameworkPropertyMetadataOptions.Journal | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,

                    new PropertyChangedCallback(OnIsCheckedChanged)));

     

            private static void OnIsCheckedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

            {

                //this is just a dummy code to see when this method is called

                string str = "hello world";

            }

     

            public bool IsButtonChecked

            {

                get { return (bool)GetValue(IsButtonCheckedProperty); }

                set { SetValue(IsButtonCheckedProperty, value); }

            }



    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.

    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. 

    Hope this helps !  
    MVP Client App
    • Als Antwort markiertClarkZeng Freitag, 6. Juni 2008 09:20
    •  

Alle Antworten

  • Freitag, 6. Juni 2008 01:17ClarkZeng TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
     Can anyone give me some suggestion?


    Thanks
  • Freitag, 6. Juni 2008 02:10Wodahs TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    Why is that causing a problem?
    John Fenton
  • Freitag, 6. Juni 2008 03:57Atul GuptaMVPTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    ClarkZeng said:

     Can anyone give me some suggestion?


    Thanks



    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
    MVP Client App
  • Freitag, 6. Juni 2008 04:10Atul GuptaMVPTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     BeantwortetEnthält Code
    I just verified the behavior by writing my own dependency property as something like below

            public static readonly DependencyProperty IsButtonCheckedProperty =

                DependencyProperty.Register("IsChecked", typeof(bool), typeof(ComboTest),

                new FrameworkPropertyMetadata(false,

                    FrameworkPropertyMetadataOptions.Journal | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,

                    new PropertyChangedCallback(OnIsCheckedChanged)));

     

            private static void OnIsCheckedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

            {

                //this is just a dummy code to see when this method is called

                string str = "hello world";

            }

     

            public bool IsButtonChecked

            {

                get { return (bool)GetValue(IsButtonCheckedProperty); }

                set { SetValue(IsButtonCheckedProperty, value); }

            }



    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.

    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. 

    Hope this helps !  
    MVP Client App
    • Als Antwort markiertClarkZeng Freitag, 6. Juni 2008 09:20
    •  
  • Freitag, 6. Juni 2008 04:37Wodahs TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    Isn't that by design.

    DependencyPropertyChangedEventArgs e
    e.NewValue <-- contains the value you should be checking.

    Still not sure why this would be a problem?


    John Fenton

    Nice demo of how it works BTW!
    • BearbeitetWodahs Freitag, 6. Juni 2008 04:56Kudos
    •  
  • Samstag, 7. Juni 2008 01:46ClarkZeng TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    Atul Gupta said:

    I just verified the behavior by writing my own dependency property as something like below

            public static readonly DependencyProperty IsButtonCheckedProperty =

                DependencyProperty.Register("IsChecked", typeof(bool), typeof(ComboTest),

                new FrameworkPropertyMetadata(false,

                    FrameworkPropertyMetadataOptions.Journal | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,

                    new PropertyChangedCallback(OnIsCheckedChanged)));

     

            private static void OnIsCheckedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

            {

                //this is just a dummy code to see when this method is called

                string str = "hello world";

            }

     

            public bool IsButtonChecked

            {

                get { return (bool)GetValue(IsButtonCheckedProperty); }

                set { SetValue(IsButtonCheckedProperty, value); }

            }



    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.

    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. 

    Hope this helps !  
    MVP Client App



     Thank you very much!

    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.
  • Samstag, 7. Juni 2008 03:37Wodahs TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    For read only on a checkbox, you just set IsEnabled="false" you probably will also want to override the template to make it display the way you want.


    John Fenton