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:
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.