简体   繁体   中英

combobox with checkbox within a WPF DataGrid

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 a ComboBox , so I'm not really sure why you can't just extend that and provide a DataTemplate to define that your ComboBoxItem s should render as Checkbox es. Try something like this:

<toolkit:DataGridTemplateColumn Header="Template">
    <toolkit:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox>
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <CheckBox IsChecked="{Binding IsChecked}" Width="20" />
                            <TextBlock Text="{Binding Program}" Width="100" />
                        </StackPanel>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
        </DataTemplate>
    </toolkit:DataGridTemplateColumn.CellTemplate>
</toolkit:DataGridTemplateColumn>

I'll leave you to finish this off.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM