2

我有一个简单的问题,我假设没有一个简单的解决方案。我需要为我的WPF DataGrid中的某些网格列创建多列ComboBox。有没有已知的最佳做法来实现这一目标?从我收集的这将需要继承DataGridComboBoxColumn以支持自定义组合框。如何在WPF DataGrid中实现多列ComboBox DataGridColumn?

我发现了一些这样的例子,但不支持EF实体(我使用Code First EF)。

任何意见非常感谢。谢谢

注意:这一切都是用C#动态完成的。我没有使用XAML来定义列。

更新:我的意思是多列简单,当你降下来组合框,我需要显示两个值“显示”,即使当然幕后的我还只是存储的ID。

请看这里: multicolumn-dropdown http://www.telerik.com/ClientsFiles/188010_multicolumn-dropdown.JPG

有了,我需要做,因为可以动态创建并添加到网格,而不是图像中显示只是简单组合一的DataGridColumn例外。

更新我终于找到了一篇关于CodeProject的文章,其中作者用我的-exact-要求开发了一个控件。它位于here。现在我唯一要解决的问题是如何让控件在使用Entity Framework时(特别是代码优先)工作。越来越近!

+0

为别人读这篇文章,我不得不修改的CustComboBox.cs的popupDataGrid_MouseDown事件文件只设置的SelectedItem,而不是的SelectedValue,因为这是导致计算器,当我绑定到XAML中的SelectedItem。 – Brent

回答

2

我找到了适用于我特定场景的解决方案。我在上一次更新中的链接中下载了包含DataGridComboBoxColumn子类的自定义多列ComboBox。基本上,我只是使用实体框架代码优先POCO做了这项工作,它解决了我的问题。这是我必须做的,以使其与POCO合作。

CustDataGridComboBoxColumn里面有一些覆盖。您只需稍微修改以下两个覆盖。我使用反射来改变设置属性,因为我不知道控件会是什么。

原始实现通过从DataRowView和SelectedValuePath中获取正确的Row来完成此操作。

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完全建立这种自定义控件,你会因为默认情况下它被设置为只读到一个setter添加到Columns属性。

//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

你能澄清你的意思是多重?

您是否希望拥有以下任何一项?

[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

我已更新我的问题。对困惑感到抱歉。 –

0

什么是“YourPropertyName”意思是: PropertyInfo info = editingElement.DataContext.GetType()。GetProperty(“YourPropertyName”,BindingFlags.Public | BindingFlags.Instance);