Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Selected Radio Button in a Group (WPF)

I have a ItemsControl in my program that contains a list of radio buttons.

<ItemsControl ItemsSource="{Binding Insertions}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <RadioButton GroupName="Insertions"/>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

How do I find the selected radio button in the group Insertions in a MVVM manner?

Most of the examples I have found on the internet involve setting individual boolean properties that you bind the IsChecked property to with the help of a converter.

Is there an equivalent of the ListBox SelectedItem that I can bind to?

like image 786
Kiang Teng Avatar asked Dec 12 '10 08:12

Kiang Teng


People also ask

How can I check if a radio button group is selected?

Using Input Radio checked property: The Input Radio checked property is used to return the checked status of an Input Radio Button. Use document. getElementById('id'). checked method to check whether the element with selected id is check or not.

How you can obtain the value of the radio button selected by the user?

To get the value of selected radio button, a user-defined function can be created that gets all the radio buttons with the name attribute and finds the radio button selected using the checked property. The checked property returns True if the radio button is selected and False otherwise.

How do I group radio buttons in XAML?

You group RadioButton controls by putting them inside the same parent container or by setting the GroupName property on each RadioButton to the same value. A RadioButton has two states: selected or cleared.

How do you add radio buttons to groups?

You group radio buttons by drawing them inside a container such as a Panel control, a GroupBox control, or a form. All radio buttons that are added directly to a form become one group. To add separate groups, you must place them inside panels or group boxes.


1 Answers

One solution that comes to mind is to add an IsChecked boolean property to your Insertion entities and bind that to the `IsChecked' property of the Radio button. This way you can check the 'Checked' radio button in View Model.

Here is a quick and dirty example.

NB: I ignored the fact that the IsChecked can also be null, you could handle that using bool? if required.

The simple ViewModel

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace WpfRadioButtonListControlTest
{
  class MainViewModel
  {
    public ObservableCollection<Insertion> Insertions { get; set; }

    public MainViewModel()
    {
      Insertions = new ObservableCollection<Insertion>();
      Insertions.Add(new Insertion() { Text = "Item 1" });
      Insertions.Add(new Insertion() { Text = "Item 2", IsChecked=true });
      Insertions.Add(new Insertion() { Text = "Item 3" });
      Insertions.Add(new Insertion() { Text = "Item 4" });
    }
  }

  class Insertion
  {
    public string Text { get; set; }
    public bool IsChecked { get; set; }
  }
}

The XAML - The code behind is not shown since it has no code other than than the generated code.

<Window x:Class="WpfRadioButtonListControlTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfRadioButtonListControlTest"
        Title="MainWindow" Height="350" Width="525">
  <Window.Resources>
    <local:MainViewModel x:Key="ViewModel" />
  </Window.Resources>
  <Grid DataContext="{StaticResource ViewModel}">
    <ItemsControl ItemsSource="{Binding Insertions}">
      <ItemsControl.ItemTemplate>
        <DataTemplate>
          <Grid>
            <RadioButton GroupName="Insertions" 
                         Content="{Binding Text}" 
                         IsChecked="{Binding IsChecked, Mode=TwoWay}"/>
          </Grid>
        </DataTemplate>
      </ItemsControl.ItemTemplate>
    </ItemsControl>
  </Grid>
</Window>
like image 90
Chris Taylor Avatar answered Sep 22 '22 01:09

Chris Taylor