如何在(WPF)DataGridComboBoxColumn中展开ComboBox? [英] How to expand the ComboBox in a (WPF) DataGridComboBoxColumn?

查看:123
本文介绍了如何在(WPF)DataGridComboBoxColumn中展开ComboBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的C#WPF应用程序(.NET 4.0)中,我有一个DataGrid,它通过包含DataGridComboBoxColumn的代码动态填充:

In my C# WPF application (.NET 4.0) I have a DataGrid dynamically filled from code including a DataGridComboBoxColumn:

public static DataGridComboBoxColumn getCboCol(string colName, Binding textBinding)
{
    List<string> statusItemsList = new StatusList();

    DataGridComboBoxColumn cboColumn = new DataGridComboBoxColumn();
    cboColumn.Header = colName;
    cboColumn.SelectedItemBinding = textBinding;
    cboColumn.ItemsSource = statusItemsList;

    return cboColumn;
}

使用BeginningEdit事件执行不同的检查。

Using the BeginningEdit event different checks are performed.

如果支票还可以,我想直接展开组合框,否则取消编辑模式:

If the checks return okay, I want to expand the combobox directly, otherwise edit mode is cancelled:

void dataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
{
    ...
    if(notOK)
        e.Cancel;
    else {
        DataGridComboBoxColumn dgCboCol = (DataGridComboBoxColumn)e.Column;
        // expand dgCboCol
    }
    ...
}






问题:如何以编程方式扩展组合框?


Questions: How to expand the combobox programmatically? Is BeginningEdit event the right place to do that?

答案:

void dataGrid_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
{
    if (e.EditingElement.GetType().Equals(typeof(ComboBox)))
    {
        ComboBox box = (ComboBox)e.EditingElement;
        box.IsDropDownOpen = true;
    }
}


推荐答案

DataGridBeginningEditEventArgs ,您可以像这样访问要编辑的单元格的生成元素:

From DataGridBeginningEditEventArgs, you could access the generated element for the cell about to be edited like this:

var contentComboBox = e.Column.GetCellContent(e.Row) as ComboBox;

但是,我不确定这能否获得您所需的实际ComboBox。 DataGrid可以为每个单元格生成两个不同的元素,具体取决于它们是否处于编辑模式(只读和读写元素)。由于 BeginningEdit 恰好在进入编辑模式之前发生,因此将获得只读元素。

However, I'm not sure that this will get the actual ComboBox you need. DataGrids can generate two different elements for each cell, depending on whether they are in edit mode (read-only and read-write elements). Since BeginningEdit happens just before entering edit mode, this will get the read-only element.

处理此问题可能是 PreparingCellForEdit ,它将在数据项上实际调用 BeginEdit 之后触发(换句话说,如果未取消 BeginningEdit ) 。在这种情况下,您可以直接通过 EditingElement 属性。

The better event to handle this in would probably be PreparingCellForEdit, which will fire after BeginEdit is actually called on the data item (in other words, if BeginningEdit was not canceled). In that event, you can access the element directly through the EditingElement property.

这篇关于如何在(WPF)DataGridComboBoxColumn中展开ComboBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆