DataGridComboBoxColumn - 单击时自动下拉

时间:2011-11-07 21:19:47

标签: c# wpf xaml datagrid

我在DataGrid中有一个DataGridComboBoxColum。 我希望能够单击一次单元格并让组合框下拉。目前我必须多次点击。

  <DataGrid AutoGenerateColumns="False" Height="148" HorizontalAlignment="Left" Margin="48,85,0,0" Name ="dg_display" VerticalAlignment="Top" Width="645"  CanUserAddRows="False" CanUserDeleteRows="False" ItemsSource="{Binding}" SelectionChanged="DgDisplaySelectionChanged">
        <DataGrid.Columns>
            <DataGridTextColumn IsReadOnly="True" Header="Symbol" Binding="{Binding Symbol}" />
            <DataGridTextColumn IsReadOnly="True" Header="Company ID" Binding="{Binding CompanyID}" />
            <DataGridComboBoxColumn IsReadOnly="False" Header="Sector" SelectedValueBinding="{Binding Sector}" DisplayMemberPath="{Binding [0]}" Visibility="Visible" >
                <DataGridComboBoxColumn.EditingElementStyle>
                    <Style TargetType="ComboBox">
                        <Setter Property="ItemsSource" Value="{Binding SectorList}" />
                    </Style>
                </DataGridComboBoxColumn.EditingElementStyle>
                <DataGridComboBoxColumn.ElementStyle>
                    <Style TargetType="ComboBox">
                        <Setter Property="ItemsSource" Value="{Binding SectorList}" />
                    </Style>
                </DataGridComboBoxColumn.ElementStyle>
            </DataGridComboBoxColumn>
        </DataGrid.Columns>
    </DataGrid>

3 个答案:

答案 0 :(得分:8)

一键式DataGridComboBoxColumn编辑+一次单击CheckboxColumn编辑
另见: https://stackoverflow.com/a/8333704/724944

XAML:

        <Style TargetType="{x:Type DataGridCell}">
            <EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown" />
            <EventSetter Event="PreviewTextInput" Handler="DataGridCell_PreviewTextInput" />
        </Style>

代码隐藏:

    private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        DataGridCell cell = sender as DataGridCell;
        GridColumnFastEdit(cell, e);
    }

    private void DataGridCell_PreviewTextInput(object sender, TextCompositionEventArgs e)
    {
        DataGridCell cell = sender as DataGridCell;
        GridColumnFastEdit(cell, e);
    }

    private static void GridColumnFastEdit(DataGridCell cell, RoutedEventArgs e)
    {
        if (cell == null || cell.IsEditing || cell.IsReadOnly)
            return;

        DataGrid dataGrid = FindVisualParent<DataGrid>(cell);
        if (dataGrid == null)
            return;

        if (!cell.IsFocused)
        {
            cell.Focus();
        }

        if (cell.Content is CheckBox)
        {
            if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow)
            {
                if (!cell.IsSelected)
                    cell.IsSelected = true;
            }
            else
            {
                DataGridRow row = FindVisualParent<DataGridRow>(cell);
                if (row != null && !row.IsSelected)
                {
                    row.IsSelected = true;
                }
            }
        }
        else
        {
            ComboBox cb = cell.Content as ComboBox;
            if (cb != null)
            {
                //DataGrid dataGrid = FindVisualParent<DataGrid>(cell);
                dataGrid.BeginEdit(e);
                cell.Dispatcher.Invoke(
                 DispatcherPriority.Background,
                 new Action(delegate { }));
                cb.IsDropDownOpen = true;
            }
        }
    }


    private static T FindVisualParent<T>(UIElement element) where T : UIElement
    {
        UIElement parent = element;
        while (parent != null)
        {
            T correctlyTyped = parent as T;
            if (correctlyTyped != null)
            {
                return correctlyTyped;
            }

            parent = VisualTreeHelper.GetParent(parent) as UIElement;
        }
        return null;
    }

答案 1 :(得分:2)

@ surfen的答案我遇到了问题,可能是因为它已经很多年了,WPF可能已经发生了很大变化。似乎DataGrid现在可以为您处理一些事情,例如在您开始输入时自动编辑文本字段。

我在我的组合框列中使用了DataGridTemplateColumn。该模板的TextBlockCellTemplate。调用BeginEdit后跟调度程序调用会导致组合框出现在可视树中。然后看起来鼠标单击被发送到组合框并且它自己打开。

这是我修改过的@ surfen代码版本:

public static class DataGridExtensions
{
    public static void FastEdit(this DataGrid dataGrid)
    {
        dataGrid.ThrowIfNull(nameof(dataGrid));

        dataGrid.PreviewMouseLeftButtonDown += (sender, args) => { FastEdit(args.OriginalSource, args); };
    }

    private static void FastEdit(object source, RoutedEventArgs args)
    {
        var dataGridCell = (source as UIElement)?.FindVisualParent<DataGridCell>();

        if (dataGridCell == null || dataGridCell.IsEditing || dataGridCell.IsReadOnly)
        {
            return;
        }

        var dataGrid = dataGridCell.FindVisualParent<DataGrid>();

        if (dataGrid == null)
        {
            return;
        }

        if (!dataGridCell.IsFocused)
        {
            dataGridCell.Focus();
        }

        if (dataGridCell.Content is CheckBox)
        {
            if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow)
            {
                if (!dataGridCell.IsSelected)
                {
                    dataGridCell.IsSelected = true;
                }
            }
            else
            {
                var dataGridRow = dataGridCell.FindVisualParent<DataGridRow>();

                if (dataGridRow != null && !dataGridRow.IsSelected)
                {
                    dataGridRow.IsSelected = true;
                }
            }
        }
        else
        {
            dataGrid.BeginEdit(args);

            dataGridCell.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => { }));
        }
    }
}

public static class UIElementExtensions
{
    public static T FindVisualParent<T>(this UIElement element)
        where T : UIElement
    {
        UIElement currentElement = element;

        while (currentElement != null)
        {
            var correctlyTyped = currentElement as T;

            if (correctlyTyped != null)
            {
                return correctlyTyped;
            }

            currentElement = VisualTreeHelper.GetParent(currentElement) as UIElement;
        }

        return null;
    }
}

答案 2 :(得分:0)

其他答案对我都不起作用。以下是对我有用的东西。

<DataGridComboBoxColumn
    Header="Example ComboBox"
    DisplayMemberPath="Name"
    SelectedValuePath="Id">
    <DataGridComboBoxColumn.EditingElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="IsDropDownOpen" Value="True" />
        </Style>
    </DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>

我不记得我从哪里得到这个解决方案。它可能来自堆栈溢出。如果有人看到原始来源,请随时发表评论。