WPF if statement based on radio button checked or not

Nallware picture Nallware · Mar 25, 2013 · Viewed 13.5k times · Source

I'm having difficulty with something which seems like it should be very simple. I'm coming from Windows Forms and starting up with WPF. I think I have a simple syntax issue but I can't seem to find a specific example for this radio button issue.

I have a radio button on my GUI for a query to run either via a map selection or a list. When load is clicked, it should perform one operation if map is selected, a different operation for list. Code looks similar to this:

private void Load_Click(object sender, RoutedEventArgs e)
{
  if (rdBtnList.Checked == true)
  {
    //do this
  }

  // if rdBtnList not checked (ie if rdBtnMap is checked)
  // do this
}

Any help would be greatly appreciated. Thanks.

Answer

Federico Berasategui picture Federico Berasategui · Mar 25, 2013

Change:

if (rdBtnList.Checked == true)

to

if (rdBtnList.IsChecked == true)

Note:

I'm coming from Windows Forms and starting up with WPF

  • You must forget everything you ever learned in winforms, and embrace MVVM. You should create a ViewModel and bind your rdBtnList.IsChecked property to some boolean value in the ViewModel, then perform your logic in there. The views' code behind is not the right place for application logic.