在我的WPF应用程序中我从动态源(XML)创建一个菜单。我将它与静态MenuItem结合起来,运行正常,但我在静态MenuItem上遇到了一些错误。菜单看起来像这样
Entity
- dynamic menu items
- separator
- static menu item
System.Windows.Data错误:4:找不到绑定源 参考' RelativeSource FindAncestor, AncestorType =' System.Windows.Controls.ItemsControl&#39 ;, AncestorLevel =' 1'&#39 ;. BindingExpression:路径= HorizontalContentAlignment; 的DataItem = NULL;目标元素是' MenuItem' (名称='&#39);目标财产 是' HorizontalContentAlignment' (键入' HorizontalAlignment')
VerticalAlignment
相同,打开菜单后,我也收到此错误
System.Windows.Data错误:40:BindingExpression路径错误: ' MenuItemName'在' object'上找不到的属性'' ModViewModel' (的HashCode = 13278932)&#39 ;. BindingExpression:路径= MenuItemName; 的DataItem =' ModViewModel' (的HashCode = 13278932);目标元素是 '菜单项' (名称='&#39);目标属性是'标题' (键入'对象')
绑定的XAML
<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>
有没有办法将静态菜单与动态MenuItem分开,以便静态MenuItem不使用ItemContainerStyle?或者是什么导致错误?如果您需要更多代码,请告诉我。
编辑:
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; }
}
答案 0 :(得分:0)
这对我有用,
<Window.Resources>
<CollectionViewSource x:Key="FilterOptionsBridge" Source="{Binding Path=MyProperty}" />
</Window.Resources>
ViewModelProperty:
public List<string> MyProperty { get; private set; }
在ViewModel的构造函数中:
MyProperty = new List<string>();
MyProperty.Add("Menu1");
菜单项:
<MenuItem>
<MenuItem.ItemsSource>
<CompositeCollection>
<CollectionContainer Collection="{Binding Source={StaticResource FilterOptionsBridge}}" />
<Separator></Separator>
<MenuItem Header="Hello"></MenuItem>
</CompositeCollection>
</MenuItem.ItemsSource>
</MenuItem>
对于使用命令,您必须创建如下所示的样式
<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>
现在看起来像,
<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>
您的VM集合,
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(); }
}
}
在我的VM构造函数中,
var menu1 = new Menuitems() { Title = new List<string>() {"Menu1","Menu2" }, Command=command, CommandParameter=commandparameter };
MyProperty = new List<Menuitems>() { menu1 };
此Link为CollectionContainer