我希望在任何给定时刻在DataGrid中选择一些行。我不希望它允许取消选择。如何实现?
答案 0 :(得分:3)
XAML
<DataGrid SelectionMode="Single" SelectionChanged="DataGrid_SelectionChanged">
...
</DataGrid>
C#
private void DataGrid_SelectionChanged(
object sender,
SelectionChangedEventArgs e)
{
DataGrid g = sender as DataGrid;
if (g != null &&
e.AddedItems.Count == 0 &&
e.RemovedItems.Count > 0)
{
this.Dispatcher.BeginInvoke((ThreadStart)delegate
{
g.SelectedItem = (Device)e.RemovedItems[0];
});
}
}