在运行时将绑定的ComboBox列添加到DataGridColumn

时间:2011-03-16 12:23:37

标签: wpf datagrid combobox

我想显示一个DataGrid,它将包含我从数据源上传到DataTable的数据。每次都会有不同的列,有些则需要使用ComboBox来表示。

如何在运行时为需要ComboBox的列设置DataGridTemplateColumn?

好的,这是我在@Meleak的帮助下得到的最接近的,几乎就是那里,只是在没有编辑网格时显示键而不是值。

public partial class MainWindow:Window {     公共字典MyDictionary {get;组; }

public MainWindow()
{
    InitializeComponent();

    //Init Dictionary
    this.MyDictionary = new Dictionary<int, string>();
    this.MyDictionary.Add(1, "Value 1");
    this.MyDictionary.Add(2, "Value 2");
    this.MyDictionary.Add(3, "Value 3");

    DataTable dt = new DataTable();

    DataColumn column = new DataColumn("MyTypeId", typeof(int));
    dt.Columns.Add(column);

    DataRow newRow = dt.NewRow();
    newRow["MyTypeId"] = 1;

    dt.Rows.Add(newRow);

    dataGrid.Columns.Add(GetNewComboBoxColumn("My Type", "MyTypeId", this.MyDictionary));

    this.DataContext = dt;
}

public static DataGridTemplateColumn GetNewComboBoxColumn(string header,
                                                  string bindingPath,
                                                  object itemsSource)
{
    DataGridTemplateColumn comboBoxColumn = new DataGridTemplateColumn();
    comboBoxColumn.Header = header;

    Binding textBinding = new Binding();
    textBinding.Path = new PropertyPath(bindingPath);

    FrameworkElementFactory textBlock = new FrameworkElementFactory(typeof(TextBlock));
    textBlock.SetValue(TextBlock.MarginProperty, new Thickness(3, 3, 3, 3));
    textBlock.SetBinding(TextBlock.TextProperty, textBinding);

    FrameworkElementFactory comboBox = new FrameworkElementFactory(typeof(ComboBox));
    comboBox.SetValue(ComboBox.MarginProperty, new Thickness(1, 1, 1, 1));
    comboBox.SetBinding(ComboBox.TextProperty, textBinding);
    comboBox.SetValue(ComboBox.SelectedValuePathProperty, "Key");
    comboBox.SetValue(ComboBox.DisplayMemberPathProperty, "Value");

    Binding itemsSourceBinding = new Binding();
    itemsSourceBinding.Source = itemsSource;
    comboBox.SetBinding(ComboBox.ItemsSourceProperty, itemsSourceBinding);

    comboBoxColumn.CellTemplate = new DataTemplate();
    comboBoxColumn.CellTemplate.VisualTree = textBlock;
    comboBoxColumn.CellEditingTemplate = new DataTemplate();
    comboBoxColumn.CellEditingTemplate.VisualTree = comboBox;

    return comboBoxColumn;
}

}

1 个答案:

答案 0 :(得分:4)

<强>更新
您可以使用DataGridComboBoxColumn并设置SelectedValueBinding以使其按您希望的方式运行。所以只需改变你所拥有的方法就可以了。

public DataGridColumn GetNewComboBoxColumn(string header,
                                           string bindingPath,
                                           object itemsSource)
{
    DataGridComboBoxColumn comboBoxColumn = new DataGridComboBoxColumn();
    comboBoxColumn.Header = header;
    comboBoxColumn.SelectedValuePath = "Key";
    comboBoxColumn.DisplayMemberPath = "Value";

    Binding binding = new Binding();
    binding.Path = new PropertyPath(bindingPath);
    comboBoxColumn.SelectedValueBinding = binding;

    Binding itemsSourceBinding = new Binding();
    itemsSourceBinding.Source = itemsSource;
    BindingOperations.SetBinding(comboBoxColumn, DataGridComboBoxColumn.ItemsSourceProperty, itemsSourceBinding);

    return comboBoxColumn;
}