Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Why does ToggleButton fail to obey binding when clicked?

Why does ToggleButton fail to obey binding when clicked?

Scheduled Pinned Locked Moved WPF
wpfhelpquestioncsharpdotnet
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RNEELY
    wrote on last edited by
    #1

    Please help. I tracked down a bug down to the fact that a toggle button in my app ignores it’s binding. I’ve simplified to the enclosed example WPF app. What I want is the BoundToChecked toggle button to only reflect the real value of ValueSource.Checked. What occurs is when the BoundToChecked toggle button is clicked it changes visual state even if ValueSource.AllowChange is false! Any suggestions or advice would be most appreciated. Thanks, Ron // ValueSource.cs using System; using System.ComponentModel; namespace ToggleButtonBound { class ValueSource : INotifyPropertyChanged { private bool _allowChange; public bool AllowChange { get { return _allowChange; } set { _allowChange = value; OnPropertyChanged("AllowChange"); } } private bool _checked; public bool Checked { get {return _checked;} set { if (AllowChange) { _checked = value; OnPropertyChanged("Checked"); } } } #region INotifyPropertyChanged Implementation public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string property) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(property)); } } #endregion } } // Window1.xaml <Window x:Class="ToggleButtonBound.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:me="clr-namespace:ToggleButtonBound" Title="ToggleButtonBound" Height="300" Width="300"> <Window.Resources> <me:ValueSource x:Key="ValueSource" /> </Window.Resources> <StackPanel> <ToggleButton Content="Unbound"/> <ToggleButton Content="BoundToChecked" IsChecked="{Binding Source={StaticResource ValueSource}, Path=Checked, Mode=Default}"/> <CheckBox Content="AllowChange" IsChecked="{Binding Source={StaticResource ValueSource}, Path=AllowChange, Mode=Default}"/> </StackPanel> </Window>

    I 1 Reply Last reply
    0
    • R RNEELY

      Please help. I tracked down a bug down to the fact that a toggle button in my app ignores it’s binding. I’ve simplified to the enclosed example WPF app. What I want is the BoundToChecked toggle button to only reflect the real value of ValueSource.Checked. What occurs is when the BoundToChecked toggle button is clicked it changes visual state even if ValueSource.AllowChange is false! Any suggestions or advice would be most appreciated. Thanks, Ron // ValueSource.cs using System; using System.ComponentModel; namespace ToggleButtonBound { class ValueSource : INotifyPropertyChanged { private bool _allowChange; public bool AllowChange { get { return _allowChange; } set { _allowChange = value; OnPropertyChanged("AllowChange"); } } private bool _checked; public bool Checked { get {return _checked;} set { if (AllowChange) { _checked = value; OnPropertyChanged("Checked"); } } } #region INotifyPropertyChanged Implementation public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string property) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(property)); } } #endregion } } // Window1.xaml <Window x:Class="ToggleButtonBound.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:me="clr-namespace:ToggleButtonBound" Title="ToggleButtonBound" Height="300" Width="300"> <Window.Resources> <me:ValueSource x:Key="ValueSource" /> </Window.Resources> <StackPanel> <ToggleButton Content="Unbound"/> <ToggleButton Content="BoundToChecked" IsChecked="{Binding Source={StaticResource ValueSource}, Path=Checked, Mode=Default}"/> <CheckBox Content="AllowChange" IsChecked="{Binding Source={StaticResource ValueSource}, Path=AllowChange, Mode=Default}"/> </StackPanel> </Window>

      I Offline
      I Offline
      Insincere Dave
      wrote on last edited by
      #2

      See this thread for a similar issue, there is a workaround posted but in your case binding the ToggleButton's Enabled property to AllowChange might be better.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups