我有一个五列的数据网格。塔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。我必须点击两次以打开组合框。有没有办法打开组合框只需点击一下鼠标进入单元格?
不明白。我不使用DataGridView标签,在我的XAML中没有使用EditMode的选项 –