如何通过单击打开comboBoxColumn

时间:2015-04-27 10:59:06

标签: c# .net wpf datagrid combobox

我的目标是使用组合框列实现数据网格,用户可以使用鼠标单击进行选择。 我已经用Google搜索了这个问题,并找到了很多可行的解决方案(herehere), UNTIL用户在第一列中扩展了组合框,然后在第2列中选择了组合框。是第一次点击时,第一个组合框只是失去焦点,只有在第二次点击后,其他组合框才会被扩展。但是,它需要单击完成。

基本上,XAML看起来像:

 <DataGrid Name="grid" SelectionMode="Extended" HeadersVisibility="Column" SelectionUnit="Cell" >
    <DataGrid.Columns>
        <DataGridComboBoxColumn Header="Name" Width="*" SelectedItemBinding="{Binding Path=Entity}" DisplayMemberPath="EntityName" >
            <DataGridComboBoxColumn.ElementStyle>
                <Style TargetType="ComboBox">
                    <Setter Property="ItemsSource" Value="{Binding Path=Entities}"/>
                </Style>
            </DataGridComboBoxColumn.ElementStyle>
            <DataGridComboBoxColumn.EditingElementStyle>
                <Style TargetType="ComboBox">
                    <Setter Property="ItemsSource" Value="{Binding Path=Entities, Mode=TwoWay}"/>                                                        
                    <Setter Property="Uid" Value="cbEntMapping"/>
                    <EventSetter Event="SelectionChanged" Handler="CbFirmChanged" />
                </Style>                                                    
            </DataGridComboBoxColumn.EditingElementStyle>
            <DataGridComboBoxColumn.CellStyle>
                <Style TargetType="{x:Type DataGridCell}">
                    <EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown" />                                                       
                </Style>                                                    
            </DataGridComboBoxColumn.CellStyle>
        </DataGridComboBoxColumn>
        <DataGridComboBoxColumn Header="Give Up" 
            Width="*"
            SelectedItemBinding="{Binding Path=GiveUp}"
            DisplayMemberPath="EntityName">
            <DataGridComboBoxColumn.ElementStyle>
                <Style TargetType="ComboBox">
                    <Setter Property="ItemsSource" Value="{Binding Path=GiveUp}"/>

                </Style>
            </DataGridComboBoxColumn.ElementStyle>
            <DataGridComboBoxColumn.EditingElementStyle>
                <Style TargetType="ComboBox">
                    <Setter Property="ItemsSource" Value="{Binding Path=GiveUp, Mode=TwoWay}"/>
                    <EventSetter Event="SelectionChanged" Handler="CbFirmChanged" />                                                        
                    <Setter Property="Uid" Value="cbEntMapping"/>
                </Style>
            </DataGridComboBoxColumn.EditingElementStyle>
            <DataGridComboBoxColumn.CellStyle>
                <Style TargetType="{x:Type DataGridCell}">
                    <EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown" />
                </Style>
            </DataGridComboBoxColumn.CellStyle>
        </DataGridComboBoxColumn>
    </DataGrid.Columns>
</DataGrid>

以下是单击编辑模式的当前实现:

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

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

        var dataGrid = grid;
        if (dataGrid == null)
            return;

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

        var cb = cell.Content as ComboBox;
        if (cb == null) return;
        grid.BeginEdit(e);
        cell.Dispatcher.Invoke(DispatcherPriority.Background, new Action(delegate { }));
        cb.IsDropDownOpen = true;
    }

1 个答案:

答案 0 :(得分:0)

从评论中移出 -

您是否尝试将带有编辑模板的templatecolumn用作组合框并在单击时设置为编辑模式?

之后,单击下面的组合打开应该可以工作..

private void UIElement_OnPreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
     combo.IsDropDownOpen = !combo.IsDropDownOpen; 
}