动态填充WPF上下文菜单

时间:2014-04-01 08:02:58

标签: c# wpf contextmenu

我在托盘图标上有一个WPF上下文菜单。我需要的是根据ViewModel中绑定的集合动态添加菜单项。

最好的方法是什么? 我试过这个

<ContextMenu>
    <ContextMenu.ItemsSource>
        <CompositeCollection>
            <MenuItem ItemsSource="{Binding Path=RegisteredWindows}">
                <MenuItem.ItemContainerStyle>
                    <Style TargetType="{x:Type MenuItem}">
                        <Setter Property="Header" Value="{Binding Path=Name}" />
                    </Style>
                </MenuItem.ItemContainerStyle>
            </MenuItem>
            <MenuItem Header="Show/Hide All" Command="{Binding Path=HideShow}" />
            <Separator />
            <MenuItem Header="Exit" Command="{Binding Path=Quit}" />
        </CompositeCollection>
    </ContextMenu.ItemsSource>
</ContextMenu>

但是我看不到文字,只有空行...

* *解决。请参阅下面的答案。

1 个答案:

答案 0 :(得分:0)

解决。

我误认为&#34;姓名&#34;窗口属性&#34;标题&#34;属性。

更改

中的绑定
...{Binding Path=Title}

它有效。