我有点卡在这里..已经搜索了两天而没有找到解决方案。
我想要实现的是DataGridComboBoxColumn
的行为。
我需要DisplayMemberPath
,SelectedValuePath
和SelectedValueBinding
属性..
我的UserControl类似于CellEditingTemplate
中的ComboBox
和CellTemplate
中的TextBlock。
我在代码中执行所有这些操作,因为这些列可能不一定存在..
这是我定义TemplateColumn的地方:
DataGridTemplateColumn tcol = new DataGridTemplateColumn();
tcol.Header = "accCust";
FrameworkElementFactory texttablF = new FrameworkElementFactory(typeof(TextTableBox));
texttablF.SetValue(TextTableBox.TableSourceProperty, accTable.DefaultView);
FrameworkElementFactory tb = new FrameworkElementFactory(typeof(TextBlock));
texttablF.SetBinding(TextBlock.TextProperty, new Binding("Account"));
tcol.CellTemplate = new DataTemplate(typeof(DataGridCell)) { VisualTree = tb };
tcol.CellEditingTemplate = new DataTemplate(typeof(DataGridCell)) { VisualTree = texttablF };
我有两个问题:
DisplayMemberPath
中指定的值,我不知道如何实现。 DependencyProperty TableSource
在通过FrameworkElement.setValue完成时无法工作..当它以正常方式完成时,它可以正常工作,
即
TextTableBox ttb = new TextTableBox();
ttb.TableSource = src_table.DefaultView;
我希望你明白我的问题是什么..我真正想要的是用我的UserControl替换DataGridComboColumn中的ComboBox ..
提前致谢:) 我很抱歉,如果这是一个糟糕的问题。我是WPF的新手并且正在做我的HighSchools ..
答案 0 :(得分:0)
因为我想要ComboBox的属性,所以我必须继承DataGridComboBoxColumn
。
下面是代码的样子:
public class DataGridTCBColumn : DataGridComboBoxColumn
{
private TableComboBox comboBox;
public DataGridTCBColumn(bool Editable)
{
comboBox = new TableComboBox() { IsEditable = Editable };
}
// Requires extra field..
public string SelectedValuePathX
{
get
{
return (string)this.GetValue(SelectedValuePathXProperty);
}
set
{
this.SetValue(SelectedValuePathXProperty, value);
}
}
static FrameworkPropertyMetadata SelectedValuePathXPropertyMeta = new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(SelectedValuePathXPropertyChanged));
public static readonly DependencyProperty SelectedValuePathXProperty =
DependencyProperty.Register("SelectedValueX", typeof(string), typeof(TableComboBox), SelectedValuePathXPropertyMeta);
private static void SelectedValuePathXPropertyChanged(DependencyObject dobj, DependencyPropertyChangedEventArgs e)
{}
protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
{
if (e.Property == DataGridTCBColumn.ItemsSourceProperty)
{
comboBox.ItemsSource = ItemsSource;
}
else if (e.Property == DataGridTCBColumn.SelectedValuePathProperty)
{
comboBox.SelectedValuePath = SelectedValuePath;
}
else if (e.Property == DataGridTCBColumn.DisplayMemberPathProperty)
{
comboBox.DisplayMemberPath = DisplayMemberPath;
}
base.OnPropertyChanged(e);
}
protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
{
return comboBox;
}
protected override object PrepareCellForEdit(FrameworkElement editingElement, RoutedEventArgs editingEventArgs)
{
DataGridCell cell = editingEventArgs.Source as DataGridCell;
if (cell != null && !string.IsNullOrEmpty(this.SelectedValuePathX))
{
//For Typed DataSet
object obj = ((DataRowView)editingElement.DataContext).Row[this.SelectedValuePathX];
comboBox.SelectedValue = obj;
}
comboBox.Focus();
return comboBox.SelectedItem;
}
protected override bool CommitCellEdit(FrameworkElement editingElement)
{
if (!string.IsNullOrEmpty(this.SelectedValuePathX) && comboBox.SelectedValue != null)
((DataRowView)editingElement.DataContext).Row[this.SelectedValuePathX] = comboBox.SelectedValue;
return true;
}
}
必须制作另一个依赖属性SelectedValuePathX
。实际上,它完成了SelectedValueBinding
的工作。然而,Binding从未起作用(尝试使用ComboBox的源代码(Microsoft Reference Source),但dint工作得很好。
希望它能帮助某人:)