如何在 DataGridCell 上创建动态上下文菜单
WPF、C#、 我有一个包含多列、多行的数据网格。我希望一行上的每个单元格都有不同的上下文菜单项。
如何做到这一点?谢谢 我有这个
<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>
,但这是针对数据网格级别的。谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我成功了,xmal 没有改变。在 contextMenuOpening 后面的代码中,
我检查单击了哪一列,基于此,我将更改 menuitem 的标题
但是,有一个新问题,所有列都是 DataGridComboBoxColumn,上下文菜单显示“全部设置为 Microsoft.Windows.Controls.DataGridCell”,每个 DataGridComboBoxColumn 都绑定到数据源,所以我不知道如何获取DataGridComboBoxColumn的selectedValue。所以我的问题是如何获取 DataGridComboBoxColumn 的选定值?
另一方面,如果我知道单击了哪一行,那么我将能够从该行中找出 selectedValue。但我也不知道如何获取单击上下文菜单的哪一行。谢谢
编辑:
我设法以这种方式得到它
var commoColumn = (obj.Content as ComboBox);
if(comboColumn != null)
{
menuitem.Header = "全部设置为" +comboColumn.Text;
菜单.Visibility = Visibility.Visible;
不
优雅,但有效。谁有更好的解决方案,请告诉我。谢谢
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