如何在DataGridCell上创建动态ContextMenu

时间:2012-01-30 16:27:27

标签: c# wpf contextmenu 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>

但这是针对数据网格级别的。谢谢

1 个答案:

答案 0 :(得分:0)

我设法使用,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,上下文菜单显示“Set all to Microsoft.Windows.Controls.DataGridCell”,每个DataGridComboBoxColumn都绑定到datasource,所以我不知道如何获取selectedValue DataGridComboBoxColumn。所以我的问题是如何获取DataGridComboBoxColumn的选定值? 另一方面,如果我可以知道单击了哪一行,那么我将能够从该行中找出selectedValue。但我不知道如何获取上下文菜单的哪一行。谢谢

编辑: 我设法以这种方式得到它                             var comboColumn =(obj.Content as ComboBox);                             if(comboColumn!= null)                             {                                 menuitem.Header =“将所有设置为”+ comboColumn.Text;                                 menu.Visibility = Visibility.Visible;                             }

不优雅,但有效。任何人都有更好的解决方案,请告诉我。谢谢