This guy wanted to do something alike with a ListBox。我相信该解决方案也可以适用于 DataGrid。
编辑
public static class DataGridRowEx
{
public static bool GetCanSelect(DependencyObject obj)
{
return (bool)obj.GetValue(CanSelectProperty);
}
public static void SetCanSelect(DependencyObject obj, bool value)
{
obj.SetValue(CanSelectProperty, value);
}
public static readonly DependencyProperty CanSelectProperty =
DependencyProperty.RegisterAttached("CanSelect", typeof(bool), typeof(DataGridRowEx), new UIPropertyMetadata(true, OnCanSelectChanged));
private static void OnCanSelectChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
var item = sender as DataGridRow;
if (item == null)
return;
if ((bool)args.NewValue)
{
item.Selected -= RowSelected;
}
else
{
item.Selected += RowSelected;
item.IsSelected = false;
}
}
private static void RowSelected(object sender, RoutedEventArgs e)
{
var item = sender as DataGridRow;
if (item == null)
return;
item.Dispatcher.BeginInvoke((Action)(()=>
item.IsSelected = false));
}
}
测试它:
public class ViewModel : INotifyPropertyChanged
{
#region INotifyPropertyChanged values
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
public List<Dummy> Elements { get; set; }
public ViewModel()
{
this.Elements = new List<Dummy>(){
new Dummy() { CanSelect =true, MyProperty = "Element1"},
new Dummy() { CanSelect =false, MyProperty = "Element2"},
new Dummy() { CanSelect =true, MyProperty = "Element3"},
new Dummy() { CanSelect =false, MyProperty = "Element4"},
new Dummy() { CanSelect =true, MyProperty = "Element5"},
new Dummy() { CanSelect =true, MyProperty = "Element6"},
new Dummy() { CanSelect =true, MyProperty = "Element7"},
new Dummy() { CanSelect =true, MyProperty = "Element8"},
new Dummy() { CanSelect =false, MyProperty = "Element9"},
};
}
}
public class Dummy
{
public bool CanSelect { get; set; }
public string MyProperty { get; set; }
public override string ToString()
{
return this.MyProperty;
}
}
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:local="clr-namespace:WpfApplication1"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="525"
Height="350"
mc:Ignorable="d">
<Window.Resources>
<Style x:Key="DataGridRowStyle" TargetType="{x:Type DataGridRow}">
<Setter Property="local:DataGridRowEx.CanSelect" Value="{Binding CanSelect}" />
</Style>
</Window.Resources>
<Window.DataContext>
<local:ViewModel />
</Window.DataContext>
<Grid x:Name="LayoutRoot">
<DataGrid ItemsSource="{Binding Elements}"
RowStyle="{DynamicResource DataGridRowStyle}"
SelectionUnit="FullRow" />
</Grid>
</Window>
即使在按 shift 时,它也适用于多选。与 ListBoxItem 解决方案的唯一显着区别是取消选择必须使用 Dispatcher.BeginInvoke 进行排队,不知道为什么。
这种方法的唯一注意事项是,如果 DataGrid 具有单个选择项,则尝试选择一个不可选择的项会取消选择当前选定的项,如果 DataGrid 具有扩展选择项,则取消选择所有项。