我找到了适用于我特定场景的解决方案。我在上一次更新中的链接中下载了包含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;
}
}
感谢您提供的观看和解答。对不起,我无法充分地提出问题,以便最初做出更有意义的判断。
为别人读这篇文章,我不得不修改的CustComboBox.cs的popupDataGrid_MouseDown事件文件只设置的SelectedItem,而不是的SelectedValue,因为这是导致计算器,当我绑定到XAML中的SelectedItem。 – Brent