我是WPF的新手。我尝试在WPF中创建动态菜单。我打算使用数据库中的数据。我在vs 2010中使用WPF应用程序。
到目前为止,我所做的是:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="850" Width="725" WindowStartupLocation="CenterScreen" WindowState="Maximized" >
<Menu>
<Menu.Resources>
<Style x:Key="ThemeMenuItemStyle" TargetType="MenuItem">
<Setter Property="Header" Value="{Binding Name}"></Setter>
<Setter Property="Command" Value="{Binding ActivateCommand}"/>
<Setter Property="IsChecked" Value="{Binding IsActive}" />
<Setter Property="IsCheckable" Value="True"/>
</Style>
</Menu.Resources>
<MenuItem Header="Themes" ItemsSource="{Binding Themes}"
ItemContainerStyle="{StaticResource ThemeMenuItemStyle}" />
</Menu>
</Window>
但是,如果将值分配给菜单,我会陷入代码的困境。
我需要完整的页面代码来实现这一目标。我计划使用list作为数据源。
答案 0 :(得分:2)
这适合我(“动态”菜单的简单示例)
视图:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="300">
<Window.Resources>
<DataTemplate DataType="local:MyData2">
<MenuItem Header="{Binding Name}"/>
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Menu>
<Menu.Resources>
<Style x:Key="ThemeMenuItemStyle" TargetType="MenuItem">
<Setter Property="Header" Value="{Binding Name}"></Setter>
<Setter Property="ItemsSource" Value="{Binding More}"></Setter>
</Style>
</Menu.Resources>
<MenuItem Header="Themes" ItemsSource="{Binding Themes}"
ItemContainerStyle="{StaticResource ThemeMenuItemStyle}" />
</Menu>
</Grid>
代码背后:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MyThemesViewModel();
}
public class MyThemesViewModel
{
ObservableCollection<MyData> _themes = new ObservableCollection<MyData>();
public ObservableCollection<MyData> Themes
{
get { return _themes; }
set { _themes = value; }
}
public MyThemesViewModel()
{
Themes.Add(new MyData("a"));
Themes.Add(new MyData("b"));
Themes.Add(new MyData("c"));
Themes.Add(new MyData("d"));
}
}
}
public class MyData
{
ObservableCollection<MyData2> _more = new ObservableCollection<MyData2>(){new MyData2("h")};
public ObservableCollection<MyData2> More {get { return _more; } }
public MyData(string name)
{
Name = name;
}
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
}
public class MyData2
{
public MyData2(string name)
{
Name = name;
}
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
}
答案 1 :(得分:0)
public class MenuService
{
private List<MenuItem> allMenuItems;
public MenuService()
{
allMenuItems = new List<MenuItem>();
}
public List<MenuItem> GetParentMenuItems()
{
List<MenuItem> parentItems = allMenuItems.FindAll(x => x.Parent == null);
return parentItems;
}
public void AddMenuItem(MenuItem item, string parentName = "")
{
if (parentName == string.Empty)
{
this.allMenuItems.Add(item);
}
else
{
MenuItem parent = allMenuItems.Find(x => x.Name == parentName);
if (parent != null)
{
item.Parent = parent;
parent.AddSubMenu(item);
}
allMenuItems.Add(item);
}
}
public void RemoveMenuItem(MenuItem menuItem)
{
foreach (MenuItem item in allMenuItems)
{
item.RemoveSubMenu(menuItem);
}
if (this.allMenuItems.Contains(menuItem))
{
this.allMenuItems.Remove(menuItem);
}
}
}
谢谢,
杰夫琼斯 领导Mobile Application Development Company