使用ItemSource时向MenuItems添加标签?

时间:2018-01-14 04:53:19

标签: c# wpf xaml mvvm data-binding

我通过将ItemSource设置为视图模型中TblCategory类型的列表来填充菜单。 TblCategory类型只是一个包含IdName变量的类。

我将菜单的DisplayMemberPath设置为“Name”,因此我有一个正确命名和显示的MenuItem列表,但是我想再做一件我不知道该怎么做的事情:

id就像每个MenuItem在其Id中拥有该类别的Tag一样。

我想我必须修改Menu.ItemContainerStyle来绑定某些内容,但我需要一些指导。

Picture for clarification

Also, the TblCategory class and table

What I would like to achieve

非常感谢任何帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

<Menu ItemsSource={Binding ...}>
   <Menu.ItemContainerStyle>
        <Style TargetType="MenuItem">
            <Setter Property="Tag" Value="{Binding Path=Id}"/>
        </Style>
    </Menu.ItemContainerStyle>  
</Menu>