Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I try to add context menu to Datagrid Header part. Here i find PART_ColumnHeadersPresenterHeadersPresenter from data grid visual tree and add context menu to header. Code given below.

C#
var contextMenu = new ContextMenu();
            contextMenu.Items.Add("Name");
            contextMenu.Items.Add("Age");
            contextMenu.Items.Add("Mark");
            var columnHeadersPresenter = this.SafeFindDescendant<DataGridColumnHeadersPresenter>(ip => ip.Name == "PART_ColumnHeadersPresenter");
            if (columnHeadersPresenter != null)
            {
                columnHeadersPresenter.ContextMenu = contextMenu;
            }</pre>



XML
<DataGrid  x:Name="dg">
            <DataGrid.Columns>
                <DataGridTextColumn Header="name"/>
                <DataGridTextColumn Header="age"/>
                <DataGridTextColumn Header="mark"/>
            </DataGrid.Columns>
        </DataGrid>




But i got context menu on column header only. No context menu on grid header other than column header


My requirement is that , context menu on grid header , not on column header only.

Thanks in advance.
Posted
Updated 16-Nov-14 23:43pm
v2
Comments
johannesnestler 17-Nov-14 8:55am    
I would recommend you edit the control-template instead of finding the target for the contextmenu through code. If you edit the Template you will see the DataGridColumnHeadersPresenter is just inside a Grid - maybe you have to "redisgn" the visual tree a little to get the desired behavior...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900