我有一个包含五列的数据网格。第1列,第2列和第3列是TextColumns,用户可以在其中输入。第4列和第5列是ComboBoxes:
<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);
}
}
这对ComboBox不起作用。我必须单击两次才能打开ComboBox。有没有办法打开ComboBox只需点击一下进入单元格?
答案 0 :(得分:0)
更改&#34; EditMode&#34; DataGridView控件的属性为&#34; EditOnEnter&#34;。这会影响所有列。
答案 1 :(得分:0)
找到了解决方法。不是很漂亮,但它到目前为止有效:
我通过获取我点击的当前列扩展了grd_Cells_Selected():
int col = PNTable.SelectedCells[0].Column.DisplayIndex;
现在我只说grd.BeginEdit(e)如果当前列是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);
}
}
}
如果某人有更聪明的解决方案我会很高兴