wpf 如何动态绑定和静态添加 MenuItem?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14489112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I dynamically bind and statically add MenuItems?
提问by webe0316
I'm binding the ItemsSource of my MenuItem to an ObservableCollection in my ViewModel. Here is my xaml:
我将 MenuItem 的 ItemsSource 绑定到 ViewModel 中的 ObservableCollection。这是我的xaml:
<MenuItem Header="_View"
ItemsSource="{Binding Windows}">
<MenuItem.ItemContainerStyle>
<Style>
<Setter Property="MenuItem.Header"
Value="{Binding Title}" />
</Style>
</MenuItem.ItemContainerStyle>
</MenuItem>
This part works great, but now I also want to add some static MenuItems to the same View MenuItem, separated with a separator. Something like this, even though I know this won't work because I can't set the items twice.
这部分效果很好,但现在我还想将一些静态菜单项添加到同一个视图菜单项中,用分隔符分隔。像这样的东西,即使我知道这行不通,因为我不能设置项目两次。
<MenuItem Header="_View"
ItemsSource="{Binding Windows}">
<MenuItem.ItemContainerStyle>
<Style>
<Setter Property="MenuItem.Header"
Value="{Binding Title}" />
</Style>
</MenuItem.ItemContainerStyle>
<Separator />
<MenuItem Header="item 1" />
<MenuItem Header="item 2" />
</MenuItem>
For now I have created a work around by adding another level to the MenuItem like this:
现在,我通过向 MenuItem 添加另一个级别来创建一个解决方法,如下所示:
<MenuItem Header="_View">
<MenuItem Header="Windows"
ItemsSource="{Binding Windows}">
<MenuItem.ItemContainerStyle>
<Style>
<Setter Property="MenuItem.Header"
Value="{Binding Title}" />
</Style>
</MenuItem.ItemContainerStyle>
</MenuItem>
<MenuItem Header="Load Layout" />
<MenuItem Header="Save Layout" />
</MenuItem>
This works fine, but I'd rather not have a sub menu if at all possible. Oh, and I'd also prefer to do this in xaml instead of code behind. Any ideas?
这很好用,但如果可能的话,我宁愿没有子菜单。哦,我也更喜欢在 xaml 中执行此操作而不是隐藏代码。有任何想法吗?
回答by sa_ddam213
You can use a CompositeCollectionto do this, you can combine different Collections and add static items in the xaml.
您可以使用 aCompositeCollection来执行此操作,您可以组合不同的 Collections 并在 xaml 中添加静态项。
Example:
例子:
Xaml:
Xml:
<Window x:Class="WpfApplication8.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="233" Width="143" Name="UI">
<Window.Resources>
<CollectionViewSource Source="{Binding ElementName=UI, Path=Windows}" x:Key="YourMenuItems"/>
</Window.Resources>
<Grid DataContext="{Binding ElementName=UI}">
<Menu Height="24" VerticalAlignment="Top">
<MenuItem Header="_View" >
<MenuItem Header="Windows">
<MenuItem.ItemsSource>
<CompositeCollection>
<CollectionContainer Collection="{Binding Source={StaticResource YourMenuItems}}" />
<MenuItem Header="Menu Item 1" />
<MenuItem Header="Menu Item 2" />
<MenuItem Header="Menu Item 3" />
</CompositeCollection>
</MenuItem.ItemsSource>
<MenuItem.ItemContainerStyle>
<Style>
<Setter Property="MenuItem.Header" Value="{Binding Title}"/>
</Style>
</MenuItem.ItemContainerStyle>
</MenuItem>
</MenuItem>
</Menu>
</Grid>
</Window>
Code
代码
public partial class MainWindow : Window
{
private ObservableCollection<MyObject> _windows = new ObservableCollection<MyObject>();
public MainWindow()
{
InitializeComponent();
Windows.Add(new MyObject { Title = "Collection Item 1" });
Windows.Add(new MyObject { Title = "Collection Item 2" });
}
public ObservableCollection<MyObject> Windows
{
get { return _windows; }
set { _windows = value; }
}
}
public class MyObject
{
public string Title { get; set; }
}
Result:
结果:



