Select whole column by clicking ColumnHeader. WPF DATAGRID

I have a data grid which has disabled sorting. What I want to achieve is - by clicking a column header I want to select all cells in this column(select whole column). I've added EventSetter to my DataGridColumnHeader to bind some method to Click event, but I have no idea how this method should be written. Any ideas?

My code:

private void columnHeaderClick(object sender, RoutedEventArgs e)
{
        var columnHeader = sender as DataGridColumnHeader;
        if (columnHeader != null)
        {
            if (dgDane.SelectedCells != null)
            {
                dgDane.SelectedCells.Clear();
            }
            foreach (var item in dgDane.Items)
            {
                dgDane.SelectedCells.Add(new DataGridCellInfo(item, columnHeader.Column));                  
            }
            dgDane.Focus();
        }
    }

I Edited the code, this one above works fine(thanks to @nit). I've added focus to DataGrid because it was necessary.

Answers


This should do the trick

private void columnHeaderClick(object sender, RoutedEventArgs e)
    {
        var columnHeader = sender as DataGridColumnHeader;
        if (columnHeader != null)
        {
            dataGrid.SelectedCells.Clear();
            foreach (var item in dataGrid.Items)
            {
                dataGrid.SelectedCells.Add(new DataGridCellInfo(item,columnHeader.Column));
            }
        }
    }

Make sure you set SelectionUnit="Cell" on DataGrid

<DataGrid x:Name="dataGrid" AutoGenerateColumns="False" IsSynchronizedWithCurrentItem="True"
            SelectionUnit="Cell" /> 

Need Your Help

InvalidValueError: setCenter: not a LatLng or LatLngLiteral: not an Object

javascript google-maps asp.net-mvc-4 model-view-controller

I am trying to get circle on a map according to the place I input in a textbox. I am getting the place but not the circle. I debugged it and it always goes to the else part which should show an error

Subclassing UIView in a map to draw lines

iphone uiview ios4 uiscrollview subclass

I have an app that displays custom maps. I use a CATiledView to display the maps.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.