2017-06-21 231 views
1

我有一个五列的数据网格。塔1,2和3是TextColumns其中用户可以输入栏4和5是组合框:DataGrid组合框一键打开

<DataGrid x:Name="myTable" DataGridCell.Selected="grd_Cells_Selected" SelectionUnit="Cell" AutoGenerateColumns="False" ColumnWidth="*" Margin="5,0,0,0" Height="Auto" VerticalAlignment ="Center" HorizontalAlignment="Center" ItemsSource="{Binding mySourceCollection}"> 

    <DataGrid.Resources> 
     <DataTemplate x:Key="myTemplate1" DataType="vm:GridItem" > 
      <ComboBox SelectedValue="{Binding something, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding sometging}" DisplayMemberPath="desc"/> 
     </DataTemplate> 
     <DataTemplate x:Key="myTemplate2" DataType="vm:GridItem"> 
      <ComboBox x:Name="cboMY" SelectedValue="{Binding something, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="desc" ItemsSource="{Binding something}" IsSynchronizedWithCurrentItem="True"/> 
     </DataTemplate> 
    </DataGrid.Resources> 

    <DataGrid.Columns> 
     <DataGridTextColumn Header="H1" Binding="{Binding h1}" /> 
     <DataGridTextColumn Header="H2" Binding="{Binding h2}" /> 
     <DataGridTextColumn Header="H3" Binding="{Binding h3}" /> 
     <DataGridTemplateColumn Header="Combo1" CellTemplate="{StaticResource myTemplate1}" CellEditingTemplate="{StaticResource myTemplate1}" /> 
     <DataGridTemplateColumn Header="Combo2" CellTemplate="{StaticResource myTemplate2}" CellEditingTemplate="{StaticResource myTemplate2}" /> 
    </DataGrid.Columns> 

</DataGrid> 

我使用

DataGridCell.Selected = “grd_Cells_Selected”

与下面的CodeBehind通过只需点击一次即可选中单元格:

private void grd_Cells_Selected(object sender, RoutedEventArgs e) { 
    if (e.OriginalSource.GetType() == typeof(DataGridCell)) { 
     // Starts the Edit on the row; 
     DataGrid grd = (DataGrid)sender; 
     grd.BeginEdit(e); 
    } 
} 

这不适用于ComboBoxes。我必须点击两次以打开组合框。有没有办法打开组合框只需点击一下鼠标进入单元格?

回答

0

将DataGridView控件的“EditMode”属性更改为“EditOnEnter”。这将影响所有列。

+0

不明白。我不使用DataGridView标签,在我的XAML中没有使用EditMode的选项 –

0

找到解决方法。不漂亮,但它至今工作:

int col = PNTable.SelectedCells[0].Column.DisplayIndex; 

现在我只说grd.BeginEdit(E)如果当前列是:

我通过获取当前列在我点击扩展grd_Cells_Selected()在DataDridTerxtColumns

整个代码到目前为止:

private void grd_Cells_Selected(object sender, RoutedEventArgs e) { 

    int col = PNTable.SelectedCells[0].Column.DisplayIndex; 
    if (col <= 2) { 
     if (e.OriginalSource.GetType() == typeof(DataGridCell)) { 
      // Starts the Edit on the row; 
      DataGrid grd = (DataGrid)sender; 
      grd.BeginEdit(e); 
     } 
    } 
} 

如果有人有一个聪明的解决方案,我会很高兴