如何在WPF DataGrid中实现多列ComboBox DataGridColumn?

3
我有一个简单的问题,但我认为没有简单的解决方案。我需要在我的WPF DataGrid中的一些网格列中使用多列ComboBox。是否有已知的最佳实践来实现这一点?从我收集到的信息来看,这将需要对DataGridComboBoxColumn进行子类化以支持自定义ComboBox。
我找到了一些示例,但不支持EF实体(我正在使用Code First EF)。
非常感谢任何建议。谢谢
注意:所有这些都是使用C#动态完成的。我没有使用XAML定义列。
更新:我所说的多列只是指当您展开ComboBox时,我需要显示两个“显示”值,即使在幕后,当然我仍然只存储ID。
请参见此处: multicolumn-dropdown http://www.telerik.com/ClientsFiles/188010_multicolumn-dropdown.JPG 除了我需要将其作为可以动态创建并添加到网格中的DataGridColumn,而不仅仅是图像中显示的简单组合框。 更新我终于在CodeProject上找到了一篇文章,作者开发了一个符合我的要求的控件。它位于这里。现在我正在尝试解决的唯一问题是如何在使用Entity Framework(具体来说是Code First)时使控件正常工作。越来越接近了!

对于其他人阅读此内容,我不得不修改CustComboBox.cs文件中的popupDataGrid_MouseDown事件,仅设置SelectedItem而不是SelectedValue,因为当我在XAML中绑定到SelectedItem时,这会导致堆栈溢出。 - Brent
3个回答

3
我已经找到了适合我的情况的解决方案。我从上面提供的链接下载了自定义多列ComboBox和DataGridComboBoxColumn子类,然后将其与Entity Framework Code-First POCOs配合使用,这解决了我的问题。以下是我使用POCOs使其正常工作所需进行的修改。
在CustDataGridComboBoxColumn中,有几个重写方法。您只需要稍微修改以下两个重写方法即可。由于不知道控件的属性,我使用反射来设置该属性。
原始实现通过使用SelectedValuePath从DataRowView获取正确的行来完成此操作。
protected override object PrepareCellForEdit(FrameworkElement editingElement, RoutedEventArgs editingEventArgs)
{
      DataGridCell cell = editingEventArgs.Source as DataGridCell;
      if (cell != null)
      {
        // Changed to support EF POCOs
        PropertyInfo info = editingElement.DataContext.GetType().GetProperty("YourPropertyName", BindingFlags.Public | BindingFlags.Instance);
        object obj = info.GetValue(editingElement.DataContext, null);
        comboBox.SelectedValue = obj;
      }
      return comboBox.SelectedItem;
}

protected override bool CommitCellEdit(FrameworkElement editingElement)
{
    // Dynamically set the item on our POCO (the DataContext).
    PropertyInfo info = editingElement.DataContext.GetType().GetProperty(“YourPropertyName”, BindingFlags.Public | BindingFlags.Instance);
    info.SetValue(editingElement.DataContext, comboBox.SelectedValue, null);
    return true;
}

此外,如果您打算完全使用代码动态创建此自定义控件而不是在XAML中创建,则必须向Columns属性添加一个setter,因为默认情况下它是只读的。
//The property is default and Content property for CustComboBox
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public ObservableCollection<DataGridTextColumn> Columns
{
    get
    {
       if (this.columns == null)
       {
           this.columns = new ObservableCollection<DataGridTextColumn>();
       }
       return this.columns;
    }
    set
    {
       this.columns = value;
    }
}

感谢提供的意见和答案。抱歉一开始没有能够准确表达问题。


0

你能澄清一下你所说的“multiple”是什么意思吗?

你是想要类似以下哪种情况吗?

[Column] (Combo) (Combo) (Combo) [Column]

or

[Column] 
(Combo) 
(Combo) 
(Combo) 
[Column]

如果是这样,您需要使用DataGridTemplateColumn类型为列实现单元格模板。

http://windowsclient.net/wpf/wpf35/wpf-35sp1-toolkit-datagrid-feature-walkthrough.aspx

您可以在XAML中设置此项,然后提供一个集合绑定到网格,它会根据需要呈现列。


0
在以下代码中,"YourPropertyName" 的含义是什么: PropertyInfo info = editingElement.DataContext.GetType().GetProperty("YourPropertyName", BindingFlags.Public | BindingFlags.Instance);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接