Multiple CheckBox controls in a DataGrid cell

Paweł Łepko 21 Reputation points
2021-02-19T13:22:30.387+00:00

Hello,

In the DataGrid control I would like to add a column in which each cell would contain a list of checkbox controls (for each cell there can be a different number of such checkboxes with different descriptions). I don't know how to define such a column with an undefined number of checkboxes in a cell.

Please help.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,685 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,342 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. DaisyTian-1203 11,616 Reputation points
    2021-02-22T03:18:06.43+00:00

    I will show you a sample of adding undefine number of CheckBox in a DataGrid cell.
    Xaml code:

     <Grid>  
            <Grid.Resources>  
                <DataTemplate x:Key="DataTemplate">  
                    <ListBox ItemsSource="{Binding Checks}">  
                        <ListBox.ItemTemplate>  
                            <DataTemplate>  
                                <WrapPanel>  
                                    <CheckBox IsChecked="{Binding Check}"/>  
                                    <TextBlock Text="{Binding CheckContent}"/>  
                                </WrapPanel>  
                            </DataTemplate>  
                        </ListBox.ItemTemplate>  
                    </ListBox>  
                </DataTemplate>  
            </Grid.Resources>  
            <DataGrid x:Name="DataGrid1" AutoGenerateColumns="False" ItemsSource="{Binding DataGridDataCollection}">  
                <DataGrid.Columns>  
                    <DataGridTemplateColumn Header="Checks" Width="150" CellTemplate="{StaticResource DataTemplate}" />  
                </DataGrid.Columns>  
            </DataGrid>  
        </Grid>  
    

    The cs code is:

    public partial class Window1 : Window, INotifyPropertyChanged  
        {  
            public event PropertyChangedEventHandler PropertyChanged;  
      
            private ObservableCollection<DataGridRowData> _DataGridDataCollection;  
            public ObservableCollection<DataGridRowData> DataGridDataCollection  
            {  
                get => _DataGridDataCollection;  
                set  
                {  
                    _DataGridDataCollection = value;  
                    OnPropertyChanged();  
                }  
            }  
      
            public Window1()  
            {  
                DataGridDataCollection = new ObservableCollection<DataGridRowData>();  
                InitializeComponent();  
                this.DataContext = this;  
                addChecks();  
            }  
      
            // adds data to the DataGrid  
            private void addChecks()  
            {  
                DataGridDataCollection.Add(  
                new DataGridRowData(new List<DataGridCellData>   
                {   
                    new DataGridCellData(false,"Content 1_1"),   
                    new DataGridCellData(true,"Content 1_2"),   
                    new DataGridCellData(false,"Content 1_3"),   
                }));  
      
                DataGridDataCollection.Add(  
                new DataGridRowData(new List<DataGridCellData>  
                {  
                    new DataGridCellData(false,"Content 2_1"),  
                    new DataGridCellData(true,"Content 2_2"),  
                    new DataGridCellData(false,"Content 2_3"),  
                    new DataGridCellData(false,"Content 2_4"),  
                    new DataGridCellData(true,"Content 2_5")  
                }));  
      
                DataGridDataCollection.Add(  
               new DataGridRowData(new List<DataGridCellData>  
               {  
                    new DataGridCellData(false,"Content 3_1"),  
                    new DataGridCellData(true,"Content 3_2")  
               }));  
            }  
      
      
            protected void OnPropertyChanged([CallerMemberName] string name = null)  
            {  
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));  
            }  
        }  
      
        public class DataGridRowData  
        {  
            public List<DataGridCellData> Checks { get; set; }  
      
            public string CheckContent { get; set; }  
            public DataGridRowData(List<DataGridCellData> checks)  
            {  
                Checks = new List<DataGridCellData>();  
                foreach (var b in checks)  
                {  
                    Checks.Add(b);  
                }  
            }  
        }  
      
        public class DataGridCellData  
        {  
            public bool Check { get; set; }  
            public string CheckContent { get; set; }  
            public DataGridCellData(bool check,string checkContent)  
            {  
                Check = check;  
                CheckContent = checkContent;  
            }  
        }  
    

    The result picture is:
    70367-capture.png

    Is it what you want? If it is not meet your needs, please point out.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments