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. MVVM Bound Radio Button Unexpected Behavior

MVVM Bound Radio Button Unexpected Behavior

Scheduled Pinned Locked Moved WPF
wpfdebuggingarchitecturehelp
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.
  • E Offline
    E Offline
    eddieangel
    wrote on last edited by
    #1

    I have a boolean value to which radio buttons are bound, like so:

                    Yes
                
                No
    

    In VM property looks like this:

        private bool isChecked;
        public bool IsChecked
        {
            get { return isChecked; }
            set
            {
                if (isChecked!= value)
                {
                    isChecked = value;
                    OnPropertyChanged("IsChecked");
                }
            }
        }
    

    If the bound value is true, when I first open the screen the Yes checkbox is checked. I can select yes or no and they will display and save correctly. However, if I open the screen with a no value, nothing is checked. I believe the problem lies in the fact that when IsChecked is false, the converter converts that value to true in order to display the No box as checked, but in doing so it sets the IsChecked property to true, which then means that the No button should not be checked. The converter is the standard bool inverter all over the internet. I didnae write this code, just trying to debug it. Cheers, --EA

    Richard DeemingR 1 Reply Last reply
    0
    • E eddieangel

      I have a boolean value to which radio buttons are bound, like so:

                      Yes
                  
                  No
      

      In VM property looks like this:

          private bool isChecked;
          public bool IsChecked
          {
              get { return isChecked; }
              set
              {
                  if (isChecked!= value)
                  {
                      isChecked = value;
                      OnPropertyChanged("IsChecked");
                  }
              }
          }
      

      If the bound value is true, when I first open the screen the Yes checkbox is checked. I can select yes or no and they will display and save correctly. However, if I open the screen with a no value, nothing is checked. I believe the problem lies in the fact that when IsChecked is false, the converter converts that value to true in order to display the No box as checked, but in doing so it sets the IsChecked property to true, which then means that the No button should not be checked. The converter is the standard bool inverter all over the internet. I didnae write this code, just trying to debug it. Cheers, --EA

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Have you tried setting Mode=OneWay on the "Nae" binding?

      <RadioButton x:Name="Nae"
      GroupName="wah"
      IsChecked="{Binding IsChecked, Converter={StaticResource boolInv}, Mode=OneWay}">


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      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