Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
794 views
in Technique[技术] by (71.8m points)

wpf - How do I dynamically bind and statically add MenuItems?

I'm binding the ItemsSource of my MenuItem to an ObservableCollection in my ViewModel. Here is my 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 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use a CompositeCollection to do this, you can combine different Collections and add static items in the xaml.

Example:

Xaml:

<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:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...