我希望在任何给定时刻在DataGrid中选择某一行.我不希望它允许取消选择.如何实现呢?
解决方法:
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];
});
}
}