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
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
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