I need to display combobox with checkbox option within a DataGrid in WPF. Please provide any solution for that.
I have tried the below code
<toolkit:DataGridTemplateColumn Header="Template">
<toolkit:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox>
<ComboBoxItem BindingGroup="{Binding Program}">
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsChecked}" Width="20" />
<TextBlock Text="{Binding Program}" Width="100" />
</StackPanel>
</ComboBoxItem>
</ComboBox>
</DataTemplate>
</toolkit:DataGridTemplateColumn.CellTemplate>
</toolkit:DataGridTemplateColumn>
It will output like this
Anyone please help to load collection of items in combobox and to correct my code.
CS code:
private void resultGrid_Loaded(object sender, RoutedEventArgs e)
{
var programs = new List<Programs>();
programs.Add(new Programs("test", false));
programs.Add(new Programs("test1", false));
programs.Add(new Programs("test2", true));
//var grid = sender as DataGrid;
resultGrid.ItemsSource = programs;
Combo.ItemsSource = programs;
}
And the Model:
public class Programs
{
public Programs(string Program, bool IsChecked)
{
this.Program = Program;
this.IsChecked = IsChecked;
}
public string Program { get; set; }
public bool IsChecked { get; set; }
}
Finaly got an idea @Sheridan mentioned :
You provided a
DataTemplate
to define that your column should render aComboBox
, so I'm not really sure why you can't just extend that and provide aDataTemplate
to define that yourComboBoxItem
s should render asCheckbox
es. Try something like this:I'll leave you to finish this off.