後臺代碼: ...
<DataGrid Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top" AutoGenerateColumns="False" ItemsSource="{Binding }" Name="dg_cards" MouseDoubleClick="dg_cards_MouseDoubleClick" > <DataGrid.Columns> <DataGridTextColumn Header="ID" Width="40" Binding="{Binding index}" IsReadOnly="True" /> <DataGridTemplateColumn Header="啟用" Width="50"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <CheckBox Name="cbStatus" MinWidth="{Binding ID}" Uid="{Binding Path=ID}" IsChecked="{Binding IsChecked}" Tag="{Binding ID}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTextColumn Header="卡口" Width="80" IsReadOnly="True" Binding="{Binding cardname}" /> <DataGridTemplateColumn Header="八埠卡" Width="80"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <CheckBox Name="cbeight" IsChecked="{Binding IsEChecked}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTextColumn Header="操作" Width="80" IsReadOnly="True" Binding="{Binding op}" /> </DataGrid.Columns> </DataGrid>
後臺代碼:
private void dg_cards_MouseDoubleClick(object sender, MouseButtonEventArgs e) { bool isEight = false; DataGrid dg = sender as DataGrid; //雙擊獲取第三列的控制項 DataGridTemplateColumn templeColumn1 = dg.Columns[3] as DataGridTemplateColumn; FrameworkElement fwElement1 = dg_cards.Columns[3].GetCellContent(dg.CurrentItem); CheckBox cBox1 = templeColumn1.CellTemplate.FindName("cbeight", fwElement1) as CheckBox; if ((bool)cBox1.IsChecked) { isEight = true; } }