2014-07-04 54 views
0

對不起,對於長標題,但我想盡可能在​​標題中加入問題。在wpf datagrid列中顯示下拉列表,以便在數據單元格中的第一次鼠標單擊時顯示下拉列表

我有一個WPF數據網格,如下面的代碼所示。網格的交易欄有一個下拉菜單。當用戶點擊單元格時,它會選擇單元格。然後他們必須再次單擊單元格,才能進入單元格編輯模式並顯示下拉列表。然後他們必須再次單擊單元格以查看下拉列表。

我想知道是否有人能夠告訴我如何在第一次點擊下拉組合列表,而不是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> 

回答

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); 
        } 
       } 

      } 
     }