获取wpf datagrid列中的下拉列表,以在数据单元格中第一次单击鼠标时显示下拉列表

时间:2014-07-04 09:51:22

标签: wpfdatagrid behavior dropdownbox

对于长标题感到抱歉,但如果可能,我想在标题中解决问题。

我有一个WPF数据网格,如下面的代码所示。网格的Trade列有一个下拉列表。当用户点击单元格时,它会选择单元格。然后,他们必须再次单击该单元格,进入单元格编辑模式并显示下拉列表。然后,他们必须第三次单击该单元格以查看下拉列表。

我想知道是否有人可以告诉我如何在第一次点击时下拉组合列表而不是3次点击,或者可能指向我想要做的事情以便启用这个。提前谢谢。

<DataGrid SelectionMode="Single" Grid.Row="0" Name="grd_S_TML" CanUserAddRows="False" TabIndex="41" MinHeight="{Binding Grd_S_TMLHeight}" ItemsSource="{Binding GrdSTmlCollection}" SelectedIndex="{Binding GrdSTmlSelectedIndex}" SelectedItem="{Binding GrdSTmlSelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="false" 
                                              behaviors:DragManagerExtended.DragOverCommand="{Binding GrdStmlDragOverCommand}" behaviors:DragManagerExtended.DragDropCommand="{Binding GrdStmlDragDropCommand}" behaviors:DragManagerExtended.IsDropTarget="True" ContextMenu="{StaticResource mnuMGridPopUp}" CellStyle="{StaticResource OverrideGridCellHighlight}" 
                                              behaviors:DragManagerExtended.IsNeedToBeDisposed="{Binding IsFormClosed}">
    <DataGrid.Columns>
        <DataGridTextColumn Header="ID" Binding="{Binding SequenceNo}" Width="0" Visibility="Collapsed" IsReadOnly="True" />
        <DataGridTextColumn Header="Status" Binding="{Binding RowStatus}" Width="0" Visibility="Collapsed" IsReadOnly="True" />
        <DataGridTemplateColumn Header="Trade" Width="201">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock HorizontalAlignment="Left" Text="{Binding Path=TradeSelectedItem.Text}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <ComboBox x:Name="cboTrades" ItemsSource="{Binding TradeCollection}" DisplayMemberPath="Text" SelectedValuePath="ID" SelectedItem="{Binding TradeSelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                        <i:Interaction.Triggers>
                            <behaviors:KeyPressedBehavior>
                                <i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window},Path=DataContext.RoleComboBoxKeyPressEvent}" />
                            </behaviors:KeyPressedBehavior>
                        </i:Interaction.Triggers>
                    </ComboBox>
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Pay Rate" Width="60">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding PayRate, StringFormat={}{0:0.00}}" Style="{StaticResource TextBlockCenterAlligned}" TextAlignment="Right" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding Path=PayRate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MaxLength="6" TextAlignment="Right" HorizontalContentAlignment="Right" />
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Qty" Width="60">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Quantity, StringFormat={}{0:0.00}}" Style="{StaticResource TextBlockCenterAlligned}" TextAlignment="Right" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding Path=Quantity, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MaxLength="6" TextAlignment="Right" HorizontalContentAlignment="Right" />
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>
        <DataGridTextColumn Header="Rate (p/hr)" Binding="{Binding Path=RateHR, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, StringFormat={}{0:0.00}}" Width="60" IsReadOnly="True" ElementStyle="{StaticResource RightAlignDataGridTextColumnStyle}"  />
        <DataGridTextColumn Header="Value (£)" Binding="{Binding ItemValue, StringFormat={}{0:0.00}}" Width="60" IsReadOnly="True" ElementStyle="{StaticResource RightAlignDataGridTextColumnStyle}"  />
    </DataGrid.Columns>
    <i:Interaction.Behaviors>
        <behaviors:DataGridColumnIndexAndCellPositionBehavior ColumnIndex="{Binding Source={StaticResource vmOrderPricing},Path=StmlColumnIndex,Mode=TwoWay}" />
        <behaviors:DataGridRowClickBehavior RowClickCommand="{Binding GrdStmlMouseDownCommand}" />
        <behaviors:DataGridCellEditEndingBehavior CellEditEndingCommand="{Binding GrdStmlAfterColUpdateCommand}" />
        <behaviors:DataGridBeginningEditBehavior BeginningEditCommand="{Binding GrdStmlBeforeColEditCommand}" />
    </i:Interaction.Behaviors>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="LostFocus">
            <i:InvokeCommandAction Command="{Binding GrdStmlLostFocusCommand}" />
        </i:EventTrigger>
        <behaviors:KeyPressedBehavior>
            <i:InvokeCommandAction Command="{Binding GrdStmlKeyPressCommand}" />
        </behaviors:KeyPressedBehavior>
        <i:EventTrigger EventName="SelectionChanged">
            <i:InvokeCommandAction Command="{Binding GrdStmlRowColChangeCommand}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <DataGrid.RowStyle>
    <Style TargetType="DataGridRow">
            <Style.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding RowStatus}" Value="Edit"></Condition>
                        <Condition Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window}, Path=DataContext.LoadSalesOnly}" Value="false"></Condition>
                    </MultiDataTrigger.Conditions>
                    <Setter Property="Background" Value="{StaticResource ROW_STYLE_HIGHLIGHT_CHANGE}" />
                </MultiDataTrigger>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding RowStatus}" Value="Add"></Condition>
                        <Condition Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window}, Path=DataContext.LoadSalesOnly}" Value="false"></Condition>
                    </MultiDataTrigger.Conditions>
                    <Setter Property="Background" Value="{StaticResource ROW_STYLE_HIGHLIGHT_NEW}" />
                </MultiDataTrigger>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding RowStatus}" Value="Delete"></Condition>
                        <Condition Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window}, Path=DataContext.LoadSalesOnly}" Value="false"></Condition>
                    </MultiDataTrigger.Conditions>
                    <Setter Property="Background" Value="{StaticResource ROW_STYLE_HIGHLIGHT_DELETE}" />
                </MultiDataTrigger>
                <DataTrigger Binding="{Binding RowStatus}" Value="View">
                </DataTrigger>
                </Style.Triggers>
    </Style>
</DataGrid.RowStyle>

2 个答案:

答案 0 :(得分:1)

好的,我已经解决了这个问题。我想在这里发帖给其他可能偶然发现这个问题的人。

我在以下依赖项属性上创建了一个行为。

public static readonly DependencyProperty IsAllowSingleClickEditProperty = DependencyProperty.RegisterAttached("IsAllowSingleClickEdit", typeof(bool), typeof(DataGridCellSingleClickEditDependency), new PropertyMetadata(false, IsAllowSingleClickEditChanged));

...在我将此事件与此方法相关联的行为中......

private static void IsAllowSingleClickEditChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        DataGridCell dataGridCell = sender as DataGridCell;
        if (dataGridCell != null)
        {
            if (e.NewValue.Equals(true))
            {
                dataGridCell.GotFocus += DataGridCellGotFocusHandler;
            }
            else
            {
                dataGridCell.GotFocus -= DataGridCellGotFocusHandler;
            }
        }
    }

...然后最后在DataGridCellGotFocusHandler ...

中使用此代码
DataGridCell cell = sender as DataGridCell;
        if (cell != null && !cell.IsEditing && !cell.IsReadOnly)
        {
            if (!cell.IsFocused)
            {
                cell.Focus();
            }

            DataGrid dataGrid = FindVisualParent<DataGrid>(cell);

            if (dataGrid != null)
            {
                dataGrid.BeginEdit(e);
                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;
                    }
                }

                Control control = GetFirstChildByType<Control>(e.OriginalSource as DataGridCell);
                if (control != null)
                {
                    control.Focus();

                    TextBox txt = control as TextBox;
                    if (txt != null)
                    {
                        txt.Select(0,txt.Text.Length);
                    }
                }

            }
        }

答案 1 :(得分:0)

与其在DataGridTemplate列中使用组合框滚动您自己的组合框,...还不值得研究DataGridComboBoxColumn

enter image description here