Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="https://stackoverflow.com/questions/13567794/how-to-disallow-the-selection-of-an-interactive-listbox-item/13588128#13588128">This guy wanted to do something alike with a ListBox</a>. I believe the solution can be adapted to work with the DataGrid as well.</p> <p><strong>EDIT</strong></p> <pre><code>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)(()=&gt; item.IsSelected = false)); } } </code></pre> <p>To test it:</p> <pre><code>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&lt;Dummy&gt; Elements { get; set; } public ViewModel() { this.Elements = new List&lt;Dummy&gt;(){ 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; } } &lt;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"&gt; &lt;Window.Resources&gt; &lt;Style x:Key="DataGridRowStyle" TargetType="{x:Type DataGridRow}"&gt; &lt;Setter Property="local:DataGridRowEx.CanSelect" Value="{Binding CanSelect}" /&gt; &lt;/Style&gt; &lt;/Window.Resources&gt; &lt;Window.DataContext&gt; &lt;local:ViewModel /&gt; &lt;/Window.DataContext&gt; &lt;Grid x:Name="LayoutRoot"&gt; &lt;DataGrid ItemsSource="{Binding Elements}" RowStyle="{DynamicResource DataGridRowStyle}" SelectionUnit="FullRow" /&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>It works with multiselection, even when pressing shift. The only significant difference from the solution for the ListBoxItem was that the unselection had to be queued using Dispatcher.BeginInvoke, not sure why. The only caveat with this approach is that trying to select an unselectable item unselects the current selected one if the DataGrid has single selection, or unselects all, if the DataGrid has extended selection.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload