Microsoft Developer Network > Página principal de foros > Windows Presentation Foundation (WPF) > Why target update before source ? A strange problem when using binding.
Formular una preguntaFormular una pregunta
 

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

  • jueves, 05 de junio de 2008 12:35ClarkZeng Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    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!

Respuestas

  • viernes, 06 de junio de 2008 4:10Atul GuptaMVPMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     RespondidaTiene código
    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
    • Marcado como respuestaClarkZeng viernes, 06 de junio de 2008 9:20
    •  

Todas las respuestas

  • viernes, 06 de junio de 2008 1:17ClarkZeng Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
     Can anyone give me some suggestion?


    Thanks
  • viernes, 06 de junio de 2008 2:10Wodahs Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    Why is that causing a problem?
    John Fenton
  • viernes, 06 de junio de 2008 3:57Atul GuptaMVPMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    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
  • viernes, 06 de junio de 2008 4:10Atul GuptaMVPMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     RespondidaTiene código
    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
    • Marcado como respuestaClarkZeng viernes, 06 de junio de 2008 9:20
    •  
  • viernes, 06 de junio de 2008 4:37Wodahs Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    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!
    • EditadoWodahs viernes, 06 de junio de 2008 4:56Kudos
    •  
  • sábado, 07 de junio de 2008 1:46ClarkZeng Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    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.
  • sábado, 07 de junio de 2008 3:37Wodahs Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    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