简体   繁体   English

WPF DataGrid中带有复选框的组合框

[英]combobox with checkbox within a WPF DataGrid

I need to display combobox with checkbox option within a DataGrid in WPF. 我需要在WPF的DataGrid中显示带有复选框选项的组合框。 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: CS代码:

  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 : Finaly有一个想法@Sheridan提到:

在此处输入图片说明

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. 您提供了一个DataTemplate来定义您的列应呈现ComboBox ,因此我不确定如何仅扩展它并提供一个DataTemplate来定义您的ComboBoxItem应该呈现为Checkbox 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. 我将离开您以完成此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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