WPF, DataGrid中的DataGridComboBoxColumn,如何单击显示下拉框
默认双击才能出现下拉框,现在客户需要单击就显示,如何解决。
step 1:
private void datagrid1_CurrentCellChanged(object sender, EventArgs e)
{
datagrid1.BeginEdit();
}
step 2:
private void datagrid1_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
{
if(e.Column.ToString() == "System.Windows.Controls.DataGridComboBoxColumn")
((ComboBox)(e.EditingElement)).IsDropDownOpen=true;
}
参考Webbert的回复,他提供了一个方法:
public class SingleClickComboBox : DataGridComboBoxColumn
{
private ComboBox combo;
protected override object PrepareCellForEdit(System.Windows.FrameworkElement editingElement, system.Windows.RoutedEventArgs editingEventArgs)
{
object x = base.PrepareCellForEdit(editingElement, editingEventArgs);
if ((editingEventArgs is MouseButtonEventArgs) && ((MouseButtonEventArgs)editingEventArgs).ChangedButton == MouseButton.Left)
{
ComboBox o = editingElement as ComboBox;
if (o != null)
o.IsDropDownOpen = true;
}
return x;
}
}