我有一个简单的问题,我认为没有一个简单的解决方案。我需要为我的WPF DataGrid中的一些网格列设置一个多列ComboBox。是否有一个已知的最佳实践来完成这一任务?从我收集到的信息来看,这将需要对DataGridComboBoxColumn进行子类化,以支持自定义ComboBox。
我已经找到了一些例子,但不支持EF实体(我使用的是Code )。
任何建议都是非常感谢的。谢谢
注意:这都是用C#动态完成的。我不使用XAML来定义列。
Update:我的意思是,当您放下ComboBox时,我需要显示两个用于“显示”的值,当然,在幕后我仍然只是在存储一个ID。
看这里:。

multicolumn-dropdown.JPG
但是,我需要作为一个可以动态创建并添加到网格中的DataGridColumn来完成这个任务,而不仅仅是图像中显示的简单组合。
Update I终于找到了一篇关于CodeProject的文章,在文章中,作者开发了一个具有我的精确需求的控件。它位于这里。现在,我试图解决的唯一问题是如何允许控件在使用实体框架(具体来说,首先是代码)时工作。越来越近了!
发布于 2011-08-24 14:43:35
我已经为我的特殊情况找到了解决方案。我从上一次更新的链接中下载了包含DataGridComboBoxColumn子类的自定义多列DataGridComboBoxColumn。基本上,我只是用实体框架代码-第一个POCOs来完成这个工作,它解决了我的问题。这是我必须做的,以使它与波科斯。
在CustDataGridComboBoxColumn内部有一些重写。您只需要稍微修改以下两个重写。我使用反射来更改设置属性,因为我不知道它将从控件中得到什么。
最初的实现通过使用DataRowView和SelectedValuePath从SelectedValuePath中获得正确的行来实现这一点。
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;
}
}感谢您提供的意见和答复。对不起,我一开始没能说出更有意义的问题。
发布于 2011-08-23 23:47:03
你能澄清你所说的多重性是什么意思吗?
你想要像下面这样的东西吗?
[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中设置它,然后向网格提供一个集合以进行绑定,以便根据需要呈现列。
发布于 2013-11-16 05:10:47
什么将是"YourPropertyName“的意思在: PropertyInfo info = BindingFlags.Public BindingFlags.Public BindingFlags.Instance);
https://stackoverflow.com/questions/7168305
复制相似问题