本文介绍了访问资源说明中的菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 如何访问资源词典中的菜单? 在运行期间,主菜单应仅显示与当前视图关联的菜单。另外,我想在相应的视图中使用一些菜单作为上下文菜单。我不想两次或两次以上写相同的菜单,所以我想把它们放到资源词典中以便在需要时重复使用它们。 我想要一个主应用程序像这样的菜单: < StackPanel DockPanel.Dock = 热门 > < 菜单 > < MenuItem 标题 = _代理 > < MenuItem 标题 = _ Connect 命令 = {Binding Path = ProxyConnection.Connect} IsChecked = {Binding Path = ProxyConnection.IsConnected} / > < MenuItem 标题 = _Disconnect 命令 = {Binding Path = ProxyConnection.Disconnect} / > < / MenuItem > < MenuItem 标题 = _ foo > < MenuItem 标题 = _ bar 命令 = {Binding Path = Foo.Bar} / > < MenuItem 标题 = _ vodoo 命令 = {Binding Path = Foo.Vodoo} / > < / MenuItem > < /菜单 > ... 我将菜单放入resitce dicitonary Menus.xaml中,如 < MenuItem 标题 = _Proxy x:Key = proxyMenu > < MenuItem 标题 = _ Connect 命令 = {Binding Path = ProxyConnection.Connect} IsChecked = {Binding Path = ProxyConnection.IsConnected} / > < MenuItem 标题 = _断开 命令 = {Binding Path = ProxyConnection.Disconnect} / > < / MenuItem > < MenuItem 标题 = _ foo x:键 = fooMenu > < MenuItem 标题 = _ bar 逗号nd = {Binding Path = Foo.Bar} / > < MenuItem 标题 = _ vodoo 命令 = {Binding Path = Foo.Vodoo} / > < / MenuItem > 我使用这样的资源目录,或者我不是吗? < Window.Resources > < ResourceDictionary > < ResourceDictionary.MergedDictionaries > < ResourceDictionary Source = Menus.xaml > < / ResourceDictionary > < / ResourceDictionary.MergedDictionaries > < / ResourceDictionary > < / Window.Resources > 那么? < StackPanel DockPanel.Dock = Top > < 菜单 > <! - - 以下引发异常 - > < MenuItem ItemsSource = {StaticResource proxyMenu} / > 我尝试了不同的绑定但没有任何工作,因为我想要的。菜单没有出现。 感谢任何提示。 Andy 解决方案 ItemsSource 属性$ c>菜单或 MenuItem 需要其他项目的集合。 所以你的绑定不起作用。 /> 您可以在 Menus.xaml中定义 a CompositeCollection MenuItem '并将这些绑定到菜单或 MenuItem 像这样: < CompositeCollection x:key = MenuTest > < MenuItem 标题 = _ Proxy > < MenuItem 标题 = _ Connect / > < MenuItem 标题 = _Disconnect / > < / MenuI tem > < / CompositeCollection > 在你的窗口中: < 菜单 ItemsSource = {StaticResource MenuTest} / > 或 < ; 菜单 > < MenuItem 标题 = TestMenu ItemsSource = {StaticResource MenuTest} / > < /菜单 > 希望这有帮助, Thomas。 How do I access menus in resource dictionaries?During runtime the main menu should only show the menus associated to the current view. Also I want to use some of the menus as context menus inside the corresponding views. I do not want to write the same menues two or more times, so I thought about putting them into resource dictionaries to reuse them if needed.I want a main application menu like this:<StackPanel DockPanel.Dock="Top" > <Menu> <MenuItem Header="_Proxy"> <MenuItem Header="_Connect" Command="{Binding Path=ProxyConnection.Connect}" IsChecked="{Binding Path=ProxyConnection.IsConnected}" /> <MenuItem Header="_Disconnect" Command="{Binding Path=ProxyConnection.Disconnect}" /> </MenuItem> <MenuItem Header="_foo"> <MenuItem Header="_bar" Command="{Binding Path=Foo.Bar}" /> <MenuItem Header="_vodoo" Command="{Binding Path=Foo.Vodoo}" /> </MenuItem> </Menu> ...I put the menus in a resorce dicitonary Menus.xaml like<MenuItem Header="_Proxy" x:Key="proxyMenu" > <MenuItem Header="_Connect" Command="{Binding Path=ProxyConnection.Connect}" IsChecked="{Binding Path=ProxyConnection.IsConnected}" /> <MenuItem Header="_Disconnect" Command="{Binding Path=ProxyConnection.Disconnect}" /></MenuItem><MenuItem Header="_foo" x:Key="fooMenu" > <MenuItem Header="_bar" Command="{Binding Path=Foo.Bar}" /> <MenuItem Header="_vodoo" Command="{Binding Path=Foo.Vodoo}" /></MenuItem>The I use resource directories like this, or am I not?<Window.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Menus.xaml"></ResourceDictionary> </ResourceDictionary.MergedDictionaries> </ResourceDictionary></Window.Resources>And then?<StackPanel DockPanel.Dock="Top" > <Menu> <!-- following throws an exception --> <MenuItem ItemsSource="{StaticResource proxyMenu}" />I tried different bindings but nothing worked as I wanted. the menus did not appear.Thanks for any hints.Andy 解决方案 Hi,the ItemsSource property of the Menu or MenuItem expects an collection of other items.So your binding will not work.You could define in the Menus.xaml a CompositeCollection of MenuItem''s and bind these to Menu or MenuItem like so:<CompositeCollection x:key="MenuTest"> <MenuItem Header="_Proxy"> <MenuItem Header="_Connect" /> <MenuItem Header="_Disconnect" /> </MenuItem></CompositeCollection>And in your Window:<Menu ItemsSource="{StaticResource MenuTest}" />or<Menu> <MenuItem Header="TestMenu" ItemsSource="{StaticResource MenuTest}" /></Menu>Hope this helps,Thomas. 这篇关于访问资源说明中的菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-24 16:23