How to create Dynamic ContextMenu on DataGridCell

2.2k views Asked by At

WPF, C#, I have a datagrid with several columns, many rows. I want each cell on a row to have different context menu item.

How to do this? thanks I have this

<UserControl.Resources>
        <ContextMenu x:Key="cellContextMenu">
            <MenuItem x:Name="menuFillUp" Header="Fill _Up" />
        </ContextMenu>
        <Style x:Key="DataGridCellStyle" TargetType="{x:Type dg:DataGridCell}">
            <Setter Property="ContextMenu" Value="{DynamicResource cellContextMenu}" />
        </Style>
        <Style x:Key="DataGridRowStyle"  TargetType="{x:Type dg:DataGridRow}">
            <Style.Triggers>
                <Trigger Property="AlternationIndex" Value="1" >
                    <Setter Property="Background" Value="Beige" />
                </Trigger>
            </Style.Triggers>
            <Setter Property="Margin" Value="0 2 0 2" />            
        </Style>
        <Style x:Key="DataGridStyle" TargetType="{x:Type dg:DataGrid}">
            <Setter Property="AlternationCount" Value="2" />
            <Setter Property="RowStyle" Value="{StaticResource DataGridRowStyle}" />
            <Setter Property="CellStyle" Value="{StaticResource DataGridCellStyle}" />
        </Style>
</UserControl.Resources>

but this is for datagrid level. thanks

1

There are 1 answers

1
toosensitive On BEST ANSWER

I managed this to work, xmal is not changed. In code behind of contextMenuOpening,
I check which column is clicked, based on that, I will change header of menuitem

     private void basketDG_ContextMenuOpening(object sender, ContextMenuEventArgs e)
    {
        DependencyObject depObj = (DependencyObject)e.OriginalSource;
        while ((depObj != null) && !(depObj is Microsoft.Windows.Controls.DataGridCell))
        {
            depObj = VisualTreeHelper.GetParent(depObj);
        }
        if (depObj == null)
        {
            return;
        }
        if (depObj is Microsoft.Windows.Controls.DataGridCell)
        {
            var obj = depObj as Microsoft.Windows.Controls.DataGridCell;

            var menu = TryFindResource("cellContextMenu") as ContextMenu;
            if (menu != null && menu.Items.Count > 0)
            {
                var menuitem = menu.Items[0] as MenuItem;
                if (menuitem != null)
                {
                    var col = obj.Column.Header;
                    if(col.Equals("Column1") || col.Equals("Column1") 
                        || col.Equals("Column3") || col.Equals("Column4"))
                    {
                        menuitem.Header = "Set all to " + obj;
                        menu.Visibility = Visibility.Visible;
                    }
                    else
                    {
                        menu.Visibility = Visibility.Hidden;
                    }
                }
            }
        }
    } 

However, there is one new question, all column are DataGridComboBoxColumn, the context menu shows "Set all to Microsoft.Windows.Controls.DataGridCell", each DataGridComboBoxColumnis bound to datasource, so I do not know how to get the selectedValue of the DataGridComboBoxColumn. So my question is how to get the selected value of the DataGridComboBoxColumn? The other way, if I can know which row is clicked, then I will be able to figure out selectedValue from that row. but I do not know how to get which row is clicked for contextmenu, either. thanks

Edit: I managed to get it this way var comboColumn = (obj.Content as ComboBox); if(comboColumn != null) { menuitem.Header = "Set all to " + comboColumn.Text; menu.Visibility = Visibility.Visible; }

Not elegant, but works. Anyone has a better solution, please let me know. thanks