Skip to content

WPF/Net 9/ Selecting Disabled Rows in DataGrid #10994

@EldHasp

Description

@EldHasp

In some scenarios it is necessary to disable some rows in the DataGrid.
Test example:

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<TestItem> 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 });
            }
        }
    }
}
<Window x:Class="CFTopics2025.SF.IIIIIIIIIgor.Topic3210679.DataGridRowDisabledTestWindow"
        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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:CFTopics2025.SF.IIIIIIIIIgor.Topic3210679"
        mc:Ignorable="d"
        Title="DataGridRowDisabledTestWindow" Height="450" Width="800"
        DataContext="{DynamicResource vm}">
    <Window.Resources>
        <local:TestItemsViewModel x:Key="vm"/>
    </Window.Resources>
    <StackPanel>
        <DataGrid x:Name="dg" ItemsSource="{Binding Items}">
            <DataGrid.RowStyle>
                <Style TargetType="DataGridRow">
                    <Setter Property="IsEnabled" Value="{Binding IsEnabled}"/>
                    <Setter Property="ToolTipService.ShowOnDisabled" Value="True"/>
                    <Setter Property="ToolTip" Value="{Binding Description}"/>
                </Style>
            </DataGrid.RowStyle>
        </DataGrid>
        <TextBlock Text="{Binding SelectedItem, ElementName=dg}"/>
    </StackPanel>
</Window>

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:

        if (dataGridCell != null && !dataGridCell.IsSelected && !dataGridCell.IsKeyboardFocusWithin)
        {
            dataGridCell.Focus();
            HandleSelectionForCellInput(dataGridCell, startDragging: false, allowsExtendSelect: true, allowsMinimalSelect: true);
        }

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    InvestigateRequires further investigation by the WPF team.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions