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; }
}