c# - 如何防止在 WPF DataGrid 中取消选择?

标签 c# .net wpf datagrid selection

我希望在任何给定时刻在 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];
        });
    }
}

关于c# - 如何防止在 WPF DataGrid 中取消选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9520539/

相关文章:

C# + WPF : text mode equivalent of old DOS libraries or curses

c# - 通用智能感知的全新完整实现

c# - 在 C# 中将时间添加到 DateTime

c# - Unity拦截基于方法注解的change call handler

c# - 我可以处理锁定的对象吗?

c# - 如何从 Services .NET 中过滤掉 WCF 异步方法

c# - AppDomain.CurrentDomain.AssemblyResolve 可能存在的信任问题或我应该注意的其他问题

C# - MySQL - 如果状态为 'a',则从列获取值

.net - 使用 Master-Detail 场景的 MVVM 陷阱

c# - 在 WPF ControlTemplate 中查找控件