locked
Why could not do OneWay binding to ToggleButton IsChecked property? RRS feed

  • Question

  • ToggleButton support ICommand, so I create many command such TogglePlayPause, ToggleMute and it work fine but i need to bind IsChecked property too so its checked state always show correct state. but when I create OneWay binding mode for ToggleButton and when I Press ToggleButton, the binding will lost.

    The question is why ToggleButton support ICommand but does not support OneWay binding? 
    I can set TwoWay binding, but it is bad idea when ToggleButton use Command, because the actual operation handled by Command and it should not be duplicated with TwoWay binding, also some times it is not possible. in my case
    Command=TogglePlayPause
    IsChecked={Bind to IsMediaPlaying}
    IsMediaPlaying should be readonly.

    So please tell me how use ToggleButton with Command and bind its IsChecked property?
    Thursday, February 3, 2011 12:54 AM

Answers

  • Hi Madnik7G,

    Thanks for your question.

    This is a known issue in WPF3.0 and it is resolved in 4.0.

    To workaround this issue, you can use a TwoWay biding whose UpdateSourceTrigger is set to Explicit.

     

    {Binding IsMediaPlaying,Mode=TwoWay,UpdateSourceTrigger=Explicit}
    

     

    Then the binding won't update the source unless you explicitly ask it to do so by calling BindingExpression.UpdateSource method.

    Hope this helps.


    Best regards,
    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Min Zhu Monday, February 14, 2011 2:10 AM
    Friday, February 4, 2011 2:56 AM

All replies

  • What if you make it a regular two-way binding instead of one-way. Then make the IsPlaying property non read-only, but you do nothing in the set accessor, like so:

        private bool _isPlaying;
        public bool IsPlaying
        {
          get { return _isPlaying; }
          set {}
        }
    
    

    Then set _isPlaying somewhere else and NotifyPropertyChanged when doing it

    This worked nicely for me. Hope it works for you as well


    Regards, Simon
    Thursday, February 3, 2011 8:32 AM
  • Thanks

    Actually I already do it, but it not look good solution, of-course it work but I should change all of my framework readonly properties just for ToggleButton.

    I think Silverlight team should solve this issue if there is no other way.

    Thursday, February 3, 2011 12:53 PM
  • Hi Madnik7G,

    Thanks for your question.

    This is a known issue in WPF3.0 and it is resolved in 4.0.

    To workaround this issue, you can use a TwoWay biding whose UpdateSourceTrigger is set to Explicit.

     

    {Binding IsMediaPlaying,Mode=TwoWay,UpdateSourceTrigger=Explicit}
    

     

    Then the binding won't update the source unless you explicitly ask it to do so by calling BindingExpression.UpdateSource method.

    Hope this helps.


    Best regards,
    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Min Zhu Monday, February 14, 2011 2:10 AM
    Friday, February 4, 2011 2:56 AM