WPF应用程序中动态和静态菜单项绑定错误
本文关键字:菜单项 绑定 错误 静态 应用程序 动态 WPF | 更新日期: 2023-09-27 18:16:22
在我的WPF应用程序中,我从动态源(XML)创建了一个菜单。我将此与静态MenuItem相结合,它运行良好,但我在静态MenuItem上得到一些错误。菜单看起来像这样
Entity
- dynamic menu items
- separator
- static menu item
System.Windows。数据错误:4:找不到要绑定的源引用"RelativeSource FindAncestor"AncestorType = ' System.Windows.Controls.ItemsControl ',AncestorLevel ="1"。BindingExpression:路径= HorizontalContentAlignment;DataItem =零;目标元素是'菜单项' (Name= ");目标属性is 'HorizontalContentAlignment' (type 'HorizontalAlignment')
与VerticalAlignment
相同,在我打开菜单后,我也得到这个错误
System.Windows。数据错误:40:BindingExpression路径错误在"对象"ModViewModel"上找不到"MenuItemName"属性(HashCode = 13278932)"。BindingExpression:路径= MenuItemName;DataItem = ' ModViewModel ' (HashCode = 13278932);目标元素是"子菜单"(Name = ");目标属性为'Header'(类型为'Object')
绑定的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>
是否有一种方法可以将静态菜单项与动态菜单项分开,使静态菜单项不使用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; }
}
这对我有用,
<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,
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 };
这个链接是CollectionContainer
的一个很好的例子