如何使用Bindings和Observable集合从具有Bindings和Observable集合的数据库表中将菜单项添加到WPF中的菜单控件(而不是上下文菜单)?我有这个菜单:
<Menu HorizontalAlignment="Left" Height="27" VerticalAlignment="Top" Width="649">
<MenuItem Header="_File">
<MenuItem Header="_Exit" Command="{Binding ExitCommand}"/>
</MenuItem>
<MenuItem Header="_MyMenu">
<MenuItem Header="_SubMenu1" Command="{Binding SubMenu1Command}" />
<MenuItem Header="_SubMenu2" Command="{Binding SubMenu2Command}" />
</MenuItem>
</Menu>
“SubMenu1”和“_SuMenu2”是数据库表中的值:
codSubMenu | SubMenuColum | CommandColumn
1_的 _ __ _ __ _ __ | SubMenu1 的 _ __ _ _ | SubMenu1Command 2 的 _ __ _ __ _ ___ | SubMenu2 _ ____ | _SubMenu2Command
我需要这样的东西:
<Menu HorizontalAlignment="Left" Height="27" VerticalAlignment="Top" Width="649"
ItemsSource="{Binding ObservableCollectionMenu}">
<MenuItem Header="_File">
<MenuItem Header="_Exit" Command="{Binding ExitCommand}"/>
</MenuItem>
<MenuItem Header="_MyMenu">
<MenuItem Header="{Binding ObservableCollectionMenu.SubMenuColumn}" Command="{Binding ObservableCollectionMenu.CommandColumn}" />
</MenuItem>
</Menu>
当我运行应用程序时,当我按下File和MyMenu选项时,菜单必须显示此内容:
文件| MyMenu菜单
退出| SubMenu1
的 的 ___ | SubMenu2
答案 0 :(得分:5)
使用菜单的ItemsSource
属性和MenuItems(在样式中)来绑定您的集合:
<Menu ItemsSource="{Binding YourCollection}" />
和
<Style TargetType="MenuItem">
<Setter Property="Header" Value="{Binding Path=Name}" />
<Setter Property="ItemsSource" Value="{Binding Path=Children}" />
</Style>
编辑:对于命令绑定,请执行以下操作:
将这样的setter添加到MenuItem的模板中:
<Setter Property="Command" Value="{Binding Path=Command}" />
将此结构用于MenuItem
视图模型:
public class BindableMenuItem
{
public string Name { get; set; }
public BindableMenuItem[] Children { get; set; }
public ICommand Command { get; set; }
}
将根项添加到BindableMenuItems
的集合中,并将此集合绑定到菜单中。
答案 1 :(得分:2)
这就是我解决它的方式,
我创建了一个MenuItem类(注意它有一个Items列表,所以你可以构建子菜单):
public class MenuItem : ModelBase<MenuItem>
{
private List<MenuItem> _Items;
public MenuItem(string header, ICommand command)
{
Header = header;
Command = command;
}
public MenuItem()
{
}
public string Header { get; set; }
public List<IMenuItem> Items
{
get { return _Items ?? (_Items = new List<IMenuItem>()); }
set { _Items = value; }
}
public ICommand Command { get; set; }
public string CommandName { get; set; }
public object Icon { get; set; }
public bool IsCheckable { get; set; }
private bool _IsChecked;
public bool IsChecked
{
get { return _IsChecked; }
set
{
_IsChecked = value;
NotifyPropertyChanged(m=>m.IsChecked);
}
}
public bool Visible { get; set; }
public bool IsSeparator { get; set; }
public string InputGestureText { get; set; }
public string ToolTip { get; set; }
public int MenuHierarchyID { get; set; }
public int ParentMenuHierarchyID { get; set; }
public string IconPath { get; set; }
public bool IsAdminOnly { get; set; }
public object Context { get; set; }
public IMenuItem Parent { get; set; }
public int int_Sequence { get; set; }
public int int_KeyIndex { get; set; }
}
观点:
<Menu DockPanel.Dock="Top" ItemsSource="{Binding Path=MainMenu}">
<Menu.ItemContainerStyle>
<Style>
<Setter Property="MenuItem.Header" Value="{Binding Path=Header}" />
<Setter Property="MenuItem.ItemsSource" Value="{Binding Path=Items}" />
<Setter Property="MenuItem.Icon" Value="{Binding Path=Icon}" />
<Setter Property="MenuItem.IsCheckable" Value="{Binding Path=IsCheckable}" />
<Setter Property="MenuItem.IsChecked" Value="{Binding Path=IsChecked,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
<Setter Property="MenuItem.Command" Value="{Binding Path=Command}" />
<!--<Setter Property="MenuItem.CommandParameter" Value="{Binding Path=IsChecked}"/>-->
<Setter Property="MenuItem.CommandParameter" Value="{Binding Path=.}"/>
<Setter Property="MenuItem.InputGestureText" Value="{Binding Path=InputGestureText}"/>
<Setter Property="MenuItem.ToolTip" Value="{Binding Path=ToolTip}" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsSeparator}" Value="true">
<Setter Property="MenuItem.Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type MenuItem}">
<Separator Style="{DynamicResource {x:Static MenuItem.SeparatorStyleKey}}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Menu.ItemContainerStyle>
</Menu>
MainMenu
是我的主ViewModel中的ObservableCollection
属性,您可以从数据库中填充该属性。
public ObservableCollection<MenuItem> MainMenu
{
get { return _MainMenu; }
set
{
_MainMenu = value;
NotifyPropertyChanged(x => x.MainMenu);
}
}
答案 2 :(得分:0)
我在XAML中没有快速解决方案。我需要从数据库中获取子菜单项,根据具体的配置文件,一些用户拥有其他只有2或3项的所有项目。独特的方法是在XAML中创建具有禁用项的菜单,将菜单引用传递给ViewModel(如果是MVVM App)并与ObservableCollection进行比较,只启用项目equals:
<menu horizontalalignment="Left" height="27" verticalalignment="Top" width="649" name="menu1">
<menuitem header="_File">
<menuitem header="_Exit" command="{Binding ExitCommand}" />
</menuitem>
<menuitem header="_MyMenu">
<menuitem header="_SubMenu1" command="{Binding Command1}" isenabled="False" />
<menuitem header="_SubMenu2" command="{Binding Command2}" isenabled="False" />
</menuitem>
</menu>
视图模型:
for (int i = 0; i < ObservableCollectionMenu.Count; i++)
{
for (int j = 0; j < ((MenuItem)menu1.Items[1]).Items.Count; j++)
{
if (((MenuItem)((MenuItem)menu1.Items[1]).Items[j]).Header.ToString().Equals(ObservableCollectionMenu[i].SubMenuColumn))
{
((MenuItem)((MenuItem)menu1.Items[1]).Items[j]).IsEnabled = true;
break;
}
}
}
感谢所有回答我问题的人,stackoverflow可以更好地帮助代码项目。