List of Exercises

RadioButton DataGrid in WPF C#

	    
<Window x:Class="Radiobutton_Datagrid.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Radiobutton_Datagrid"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
  <Grid>
    <GroupBox Header="Variable List" Margin="20">
      <DataGrid x:Name="dg" AutoGenerateColumns="False">
        <DataGrid.Columns>
          <DataGridTextColumn x:Name="id" Header="ID" Width="40" Binding="{Binding id}"/>
          <DataGridTemplateColumn Header="Selection">
            <DataGridTemplateColumn.CellTemplate>
              <DataTemplate>
                <RadioButton x:Name="chk" Content="{Binding nam}" GroupName="gr1" Width="120" IsChecked="{Binding chkVal,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"/>
              </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
          </DataGridTemplateColumn>
          <DataGridTextColumn x:Name="corr" Header="Dimension" Width="140" Binding="{Binding dim}"/>
        </DataGrid.Columns>
      </DataGrid>
    </GroupBox>
  </Grid>
</Window>
//==============================
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Radiobutton_Datagrid
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainWindow : Window
  {
    ObservableCollection<selectVar> data { get; set; }
    public MainWindow()
    {
      InitializeComponent();

      data = new ObservableCollection<selectVar>();
      for (int i = 0; i < 10; i++)
        data.Add(new selectVar
        {
          id = i + 1,
          chkVal = false,
          nam = "Item" + (i + 1).ToString(),
          dim = string.Format("{0} \u2717 {1}", i * 3, i),
        });
      data[0].chkVal = true;
      dg.ItemsSource = data;
    }

    private class selectVar : INotifyPropertyChanged
    {
      public int id { get; set; }
      private string _nam;
      public string nam { get { return _nam; } set { _nam = value; } }
      public string dim { get; set; }
      private bool _chkVal;
      public bool chkVal
      {
        get
        {
          return _chkVal;
        }
        set
        {
          _chkVal = value;
        }
      }

      public event PropertyChangedEventHandler PropertyChanged;
    }

  }
}
		
	 


Download the project of Visual Studio 2022 in DropBox Download


How to add RadioButton Column in a DataGrid WPF C#

Video Thumbnail