dotnet / dotnet/wpf

WPF/Net 9/ Selecting Disabled Rows in DataGrid

Open
#10,994 0 comments 1 reaction 0 assignees View on GitHub
Investigate
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

In some scenarios it is necessary to disable some rows in the DataGrid.
Test example:
```cs
using System.Windows;

namespace CFTopics2025.SF.IIIIIIIIIgor.Topic3210679
{
public partial class DataGridRowDisabledTestWindow : Window
{
public DataGridRowDisabledTestWindow()
{
InitializeComponent();
}
}

public class TestItem
{
public string? Name { get; set; }
public string? Description { get; set; }
public bool IsEnabled { get; set; }

public override string ToString()
=> $"{{{Name}: {Description}}}";
}

public class TestItemsViewModel
{
private const string data = @"WPF supports a broad set of application development features, including an application model, resources, controls, graphics, layout, data binding and documents. WPF uses the Extensible Application Markup Language (XAML) to provide a declarative model for application programming.";

public List Items { get;}

public TestItemsViewModel()
{
var split = data.Split(", \r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
Items = [];
for (int i = 1; i < split.Length; i+=2)
{
Items.Add(new TestItem() { Name = split[i - 1], Description = split[i], IsEnabled = Random.Shared.Next(2) == 0 });
}
}
}
}
```
```xml








<Setter Property="IsEnabled" Value="{Binding IsEnabled}"/>
<Setter Property="ToolTipService.ShowOnDisabled" Value="True"/>
<Setter Property="ToolTip" Value="{Binding Description}"/>




```

The selection of rows in such a way is implemented as expected only for Enabled rows. And for the left mouse button this is indeed the case. But what is unexpected is that the disabled rows can be selected with the right mouse button.

I think that this is unlikely to be the intended behavior and it simply escaped attention during testing.

To the best of my experience, I have determined that this selection is made possible by the processing of the Context Menu call at the DataGrid level.
The starting point of this processing is in these lines:
```cs
if (dataGridCell != null && !dataGridCell.IsSelected && !dataGridCell.IsKeyboardFocusWithin)
{
dataGridCell.Focus();
HandleSelectionForCellInput(dataGridCell, startDragging: false, allowsExtendSelect: true, allowsMinimalSelect: true);
}
```

![Image](https://github.com/user-attachments/assets/e290a9c2-0a01-41ec-9f9e-8d6fc0235e8e)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.