How to make DataGrid Row really fully selectable (clicking on non-cell area)

Posted by Samuel on Stack Overflow See other posts from Stack Overflow or by Samuel
Published on 2013-10-20T09:37:15Z Indexed on 2013/10/20 9:54 UTC
Read the original article Hit count: 194

Filed under:
|

The question might be a little misleading.

Here is a screenshot of a DataGrid that has some dummy values (code provided below) DGV

Is there a way to make the white area not covered by a cell clickable? My intention: I want to have full row selection. This can be achieved by SelectionUnit="FullRow" which is fine but how can I make the white area implicitly select the entire row without expanding available cells in width and avoiding code behind

Here is the repro code: Xaml:

<Window x:Class="DGVRowSelectTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" DataContext="{Binding RelativeSource={RelativeSource Self}}">
   <DataGrid ItemsSource="{Binding Names}" SelectionMode="Single" SelectionUnit="FullRow" >

   </DataGrid>
</Window>

Dummy Code behind of it (just sets the two entries up)

using System.Collections.Generic;
using System.Windows;

namespace DGVRowSelectTest
{
    public partial class MainWindow : Window
    {
        private IList<KeyValuePair<string, string>> _names = new List<KeyValuePair<string, string>>{new KeyValuePair<string, string>("A1", "A2"),new KeyValuePair<string, string>("B1","B2")};
        public IList<KeyValuePair<string, string>> Names{get { return _names; }set { _names = value; }}

        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf