简体   繁体   English

C#WPF上下文菜单数据绑定

[英]C# WPF Context Menu Data Binding

I'm trying to create a dynamic Context Menu in the WPF DataGrid. 我正在尝试在WPF DataGrid中创建动态上下文菜单。 The following are the issues that I need help: 以下是我需要帮助的问题:

1) Root Menu Item Header are not bind with ViewModel while the submenu works fine. 1)子菜单运行正常时,根菜单项标题未与ViewModel绑定。

2) The submenu always pop up on the left side instead of the right. 2)子菜单始终在左侧而不是右侧弹出。 How can I fix this with style? 如何解决样式问题?

<DataGrid.ContextMenu>
<ContextMenu ItemsSource="{Binding PackageCM.Members}" HasDropShadow="True" Placement="Right">
    <ContextMenu.ItemContainerStyle>
        <Style TargetType="MenuItem">
            <Setter Property="Header" Value="{Binding CategoryName}" />
        </Style>
    </ContextMenu.ItemContainerStyle>
    <ContextMenu.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Items}">
            <MenuItem Header="{Binding DisplayName}" Command="{Binding AllPackagesVM.OpenCOBAPackageCommand, Source={StaticResource Locator}}"></MenuItem>
        </HierarchicalDataTemplate>
    </ContextMenu.ItemTemplate>
</ContextMenu>

Root Menu Item Header are not being bind. 根菜单项标题未绑定。

Basically, Context Menu is binding to the PackageCM.Members with has a list of Category object and I want to display the CategoryName on the Context Menu root. 基本上,上下文菜单绑定到PackageCM.Members具有类别对象列表,我想在上下文菜单根目录上显示CategoryName。 Following that, Each Category contains a list of Items which will be showed as submenu. 之后,每个类别都包含一个项目列表,这些项目将显示为子菜单。

Thanks in advance for help. 在此先感谢您的帮助。

First, your ContextMenu.ItemTemplate definition is incorrect, when you set an ItemSource for ContextMenu you do not define MenuItems yourself because actually ContextMenu will wrap this content inside another MenuItem. 首先,您的ContextMenu.ItemTemplate定义不正确,当您为ContextMenu设置ItemSource时,您不会自己定义MenuItems,因为实际上ContextMenu会将此内容包装在另一个MenuItem中。 So you need to change your template to something like this: 因此,您需要将模板更改为以下内容:

<ContextMenu ItemsSource="{Binding PackageCM.Members}" ...>
    <ContextMenu.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Items}">
            <TextBlock Text="{Binding DisplayName}"></TextBlock >
        </HierarchicalDataTemplate>
    </ContextMenu.ItemTemplate>
</ContextMenu>

You need to put a TextBlock instead of MenuItem because you want to display a text in your ContextMenu menus and bind its Text property to a propety in your model. 您需要放置一个TextBlock而不是MenuItem因为您想在ContextMenu菜单中显示文本并将其Text属性绑定到模型中的属性。 But so this to work, the model used for root menus and model used for sub-menus must have a property that is named equally, in your case it is DisplayName for sub-menus so in your root menus model must also have a property named DisplayName , This property is bound to Text property of the TextBlock . 但是要正常工作,用于根菜单的模型和用于子菜单的模型必须具有一个同名的属性,在您的情况下,它是子菜单的DisplayName ,因此在您的根菜单中模型还必须具有一个名为DisplayName ,此属性绑定到TextBlock Text属性。

You need to do some renaming in your models or introduce an new propety named DisplayName in Category model. 您需要在模型中进行一些重命名,或者在Category模型中引入一个名为DisplayName的新属性。 So your models would have a common propety something like in this snippet: 因此,您的模型将具有以下共同点:

// for root menu
public class Category
{
    public string CategoryName { get; }
    public string DisplayName => CategoryName;
    ...
}

// for submenus
public class Item
{
    public string DisplayName { get; }
    ...
}

Hopefully this explanation helps you understand the problem for missing header values. 希望这种解释可以帮助您理解缺少标头值的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM