如何在 DataGridCell 上创建动态上下文菜单

发布于 2024-12-29 20:04:50 字数 1330 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仙气飘飘 2025-01-05 20:04:50

我成功了,xmal 没有改变。在 contextMenuOpening 后面的代码中,
我检查单击了哪一列,基于此,我将更改 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;
                    }
                }
            }
        }
    } 

但是,有一个新问题,所有列都是 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

     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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文