1

in my WPF Application I create a Menu from a dynamic source (XML). I combined this with a static MenuItem, it runs fine but I get some errors on the static MenuItem. The Menu looks like this

Entity
- dynamic menu items
- separator
- static menu item

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')

Same for VerticalAlignment and after I open the Menu, I get this error too

System.Windows.Data Error: 40 : BindingExpression path error: 'MenuItemName' property not found on 'object' ''ModViewModel' (HashCode=13278932)'. BindingExpression:Path=MenuItemName; DataItem='ModViewModel' (HashCode=13278932); target element is 'MenuItem' (Name=''); target property is 'Header' (type 'Object')

The XAML for the binding

<MenuItem Header="_Entity">
    <MenuItem.ItemsSource>
        <CompositeCollection>
            <CollectionContainer Collection="{Binding Source={StaticResource MenuItems}}" />
            <Separator></Separator>
            <MenuItem Header="Edit Templates"/>
        </CompositeCollection>
    </MenuItem.ItemsSource>
    <MenuItem.ItemContainerStyle>
        <Style TargetType="MenuItem">
            <Setter Property="MenuItem.Header" Value="{Binding MenuItemName}"/>
            <Setter Property="CommandParameter" Value="{Binding Components}"/>
        </Style>
     </MenuItem.ItemContainerStyle>
</MenuItem>

Is there a way to separate the static from the dynamic MenuItem so the static MenuItem doesn't use the ItemContainerStyle? Or what causes the errors? If you need more Code, please tell me.

EDIT:

public ObservableCollection<Models.EntityMenuItem> MenuItems
{
    get { return _menuItem; }
    set
    {
        _menuItem = value;
        OnPropertyChanged();
    }
}

public class EntityMenuItem
{
    public string MenuItemName { get; set; }
    public Dictionary<string,bool> Components { get; set; }
}
vice_versa
  • 99
  • 1
  • 9
  • Check [This](http://stackoverflow.com/a/6446923/2470362) – Abin Oct 06 '15 at 19:08
  • Sorry, I'm kinda lost with your answer, if i leave out the static MenuItem "Edit Templates" I dont get any errors. So my question is, why is the static MenuItem causing these errors, it's like its trying to set the header property of the static MenuItem to `MenuItemName`. – vice_versa Oct 06 '15 at 19:57
  • You mean to say if you remove `` from your xaml it works fine ? – Abin Oct 06 '15 at 20:13
  • Yes, then I get no errors at all in the Debugger window. And that confuses me a bit. I mean it works also with the static MenuItem but then I get these errors in the Debugger window. I've done the same like with [link](http://stackoverflow.com/questions/14489112/how-do-i-dynamically-bind-and-statically-add-menuitems) – vice_versa Oct 06 '15 at 20:17
  • try with out binding and check i have posted code in answer. check it. – Abin Oct 06 '15 at 20:28

1 Answers1

0

This works for me,

 <Window.Resources>
    <CollectionViewSource x:Key="FilterOptionsBridge" Source="{Binding Path=MyProperty}" />
 </Window.Resources>

ViewModelProperty:

public List<string> MyProperty { get; private set; }

In Constructor of ViewModel:

MyProperty = new List<string>();
        MyProperty.Add("Menu1");

MenuItem:

  <MenuItem>
     <MenuItem.ItemsSource>
        <CompositeCollection>
           <CollectionContainer Collection="{Binding Source={StaticResource FilterOptionsBridge}}" />
            <Separator></Separator>
            <MenuItem Header="Hello"></MenuItem>
        </CompositeCollection>                            
      </MenuItem.ItemsSource>
    </MenuItem>

For using commands you have to create style like below,

 <Style TargetType="MenuItem" >
     <Setter Property="Header" Value="{Binding Path=Title}"/>         
     <Setter Property="Command" Value="{Binding Path=Command}"/>
     <Setter Property="CommandParameter" Value="{Binding Path=CommandParameter}"/>
 </Style>

now here it looks like,

 <MenuItem>
    <MenuItem.Style>
       <Style TargetType="MenuItem">                            
          <Setter Property="Command" Value="{Binding Path=Command}"/>
          <Setter Property="CommandParameter" Value="{Binding Path=CommandParameter}"/>
       </Style>
    </MenuItem.Style>
  <MenuItem.ItemsSource>
 <CompositeCollection>
     <CollectionContainer Collection="{Binding Path=Title, Source={StaticResource FilterOptionsBridge}}" />
        <Separator></Separator>
            <MenuItem Header="Hello"></MenuItem>
      </CompositeCollection>                            
     </MenuItem.ItemsSource>
    </MenuItem>

Your VM collection like,

 public List<Menuitems> MyProperty { get; private set; }

Menuitems.cs like,

 public class Menuitems //Impliment INotifyPropertyChanged Interface
    {
        private List<string> _Title = new List<string>();
        private ICommand _Command;
        private object _Commandparameter;            
        public List<string> Title
        {
            get { return _Title; }
            set { _Title = value; NotifyPropertyChanged(); }
        }
        public ICommand Command
        {
            get { return _Command; }
            set { _Command = value; NotifyPropertyChanged(); }
        }
        public object CommandParameter
        {
            get { return _Commandparameter; }
            set { _Commandparameter = value; NotifyPropertyChanged(); }
        }           
    }

In my VM constructor,

 var menu1 = new Menuitems() { Title = new List<string>() {"Menu1","Menu2" }, Command=command, CommandParameter=commandparameter };            
        MyProperty = new List<Menuitems>() { menu1 };

This Link gives good example for CollectionContainer

Abin
  • 2,868
  • 1
  • 23
  • 59
  • What do I have to put there instead of `MENU` because I get an invalid markup error in the XAML and when I run it I get > System.Windows.Data Error: 40 : BindingExpression path error: 'MenuItemName' property not found on 'object' ''Char' (HashCode=5046349)'. BindingExpression:Path=MenuItemName; DataItem='Char' (HashCode=5046349); target element is 'MenuItem' (Name=''); target property is 'Header' (type 'Object') – vice_versa Oct 06 '15 at 20:38
  • Have you tried with out binding ? and did you get some menu coming up ? – Abin Oct 06 '15 at 20:39
  • can you show me your `{StaticResource MenuItems}` code/Xaml – Abin Oct 06 '15 at 20:40
  • Yes I can `` – vice_versa Oct 06 '15 at 20:41
  • Edited the answer. try that. – Abin Oct 06 '15 at 20:45
  • Then I get `System.Windows.Data Error: 40 : BindingExpression path error: 'MenuItems' property not found on 'object' ''ListCollectionView' ...` – vice_versa Oct 06 '15 at 20:47
  • can you show me the property `MenuItems` what type is that ? – Abin Oct 06 '15 at 20:55
  • [this](http://stackoverflow.com/a/6446923/2470362) one tells you how to bind `CollectionContainer` I have edited the answer based on that. is your `EntityMenuItem` is type string ? – Abin Oct 06 '15 at 21:05
  • [CollectionContainer.Collection Property](https://msdn.microsoft.com/en-us/library/system.windows.data.collectioncontainer.collection(v=vs.110).aspx) is just an `IEnumerable ` – Abin Oct 06 '15 at 21:09
  • Yes, so is my `MenuItems` property on my ModViewModel class – vice_versa Oct 06 '15 at 21:11
  • [Check this](http://wilberbeast.com/2011/05/31/compositecollection-binding-problem/) too. – Abin Oct 06 '15 at 21:12
  • I did that already as you can see above, set the `CollectionViewSource` – vice_versa Oct 06 '15 at 21:20
  • Try the one now i have edited the answer. it works for me. `MenuItems` should be a collection of strings i believe. – Abin Oct 06 '15 at 21:33
  • This works, yes, but then I can not have a class `MenuItem` with properties for my `CommandParameters`, thats why I used the `ItemContainerStyle` to set the Header property to the `MenuItemName` property of `MenuItem` class – vice_versa Oct 06 '15 at 21:45
  • As soon as I start to add the Style to the MenuItem I get errors in the Debugger – vice_versa Oct 07 '15 at 09:03
  • I would say understand the concept and go ahead with doing each stuff. – Abin Oct 07 '15 at 13:55
  • I do understand the concept, and as I said, I only get these Debugger errors when I try to add a static MenuItem to the `CompositeCollection`, with your code, you could try the same, add a style for your MenuItem and you will get these binding errors too for the static MenuItem, I have tried it myself, because the static MenuItem doesn't know about `{Binding Path=Title}` or any other binding. I could go easily around by making the collection a SubMenu but that's not my intent to do. – vice_versa Oct 07 '15 at 14:06
  • OK but little bit of research will give you more confident. i have posted the whole code which will work with commands, commandparemeters. etc. It works for me you can follow the same path. – Abin Oct 07 '15 at 14:32
  • Fun fact, I just closed and reopened my solution and all the Debugger errors about missing binding and stuff are gone now ... – vice_versa Oct 07 '15 at 15:09
  • LOL is it that simple. – Abin Oct 07 '15 at 15:12