赞
踩
WPF Menu动态绑定View
例如:当我们有好几个上位机程序共用一个解决方案的时候,每个设备上的功能有些许的不同,需要开放不同的菜单栏选项。
可参考官方教程:https://learn.microsoft.com/zh-cn/dotnet/desktop/wpf/data/data-templating-overview?view=netframeworkdesktop-4.8
HierarchicalDataTemplate帮助文档:https://learn.microsoft.com/zh-cn/dotnet/api/system.windows.hierarchicaldatatemplate?view=windowsdesktop-7.0
例如:通过数据库去动态绑定需要开放的菜单栏选项:(示例是未配置数据库,单纯模拟)
MainWindowViewModel 中设置
using Prism.Commands; using Prism.Mvvm; using Prism.Regions; using System.Collections.ObjectModel; namespace TestViews.ViewModels { public class MainWindowViewModel : BindableBase { private string _title = "Prism Application"; private readonly IRegionManager _regionManager; public DelegateCommand<string> NavigateCommand { get; private set; } public string Title { get { return _title; } set { SetProperty(ref _title, value); } } private ObservableCollection<Menus> _menusOne = new ObservableCollection<Menus>(); public ObservableCollection<Menus> MenusOne { get { return _menusOne; } set { SetProperty(ref _menusOne, value); } } private void Navigate(string navigatePath) { if (navigatePath != null) _regionManager.RequestNavigate("ContentRegion", navigatePath); } public MainWindowViewModel() { MenusOne = new ObservableCollection<Menus> { new Menus { Content = "主页", //IsChecked = true, //CommandParameter = "home", //NavigateCommand = new DelegateCommand<string>(Navigate), PageList = new ObservableCollection<NavigateItemModel> { new NavigateItemModel { Content = "数据", IsChecked = true, CommandParameter = "data", NavigateCommand = new DelegateCommand<string>(Navigate), } } }, new Menus { Content = "视图", //IsChecked = true, //CommandParameter = "view", //NavigateCommand = new DelegateCommand<string>(Navigate), PageList = new ObservableCollection<NavigateItemModel> { new NavigateItemModel { Content = "日志", IsChecked = true, CommandParameter = "log", NavigateCommand = new DelegateCommand<string>(Navigate), } } }, new Menus { Content = "时间", //IsChecked = true, //CommandParameter = "time", //NavigateCommand = new DelegateCommand<string>(Navigate), PageList = new ObservableCollection<NavigateItemModel> { new NavigateItemModel { Content = "运行", IsChecked = true, CommandParameter = "run", NavigateCommand = new DelegateCommand<string>(Navigate), } } }, }; } } }
XAML 中配置
<Window x:Class="TestViews.Views.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:prism="http://prismlibrary.com/" xmlns:local="clr-namespace:TestViews" prism:ViewModelLocator.AutoWireViewModel="True" Title="{Binding Title}" Height="350" Width="525" > <Window.Resources> <Style x:Key="menu1" TargetType="Menu"> <Setter Property="HorizontalAlignment" Value="Center"/> <Setter Property="FontSize" Value="30"/> <!--<Setter Property="FontWeight" Value="ExtraBlack"/>--> </Style> </Window.Resources> <Grid> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Grid Grid.Row="0"> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Menu Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="10" ItemsSource="{Binding MenusOne}"> <!--<Menu.Resources> <Style TargetType="Menu"> <Setter Property="FontSize" Value="20"/> </Style> </Menu.Resources>--> <Menu.ItemTemplate> <HierarchicalDataTemplate ItemsSource="{Binding PageList}"> <TextBlock Text="{Binding Content}"/> </HierarchicalDataTemplate> </Menu.ItemTemplate> <Menu.ItemContainerStyle> <Style TargetType="MenuItem"> <Setter Property="FontSize" Value="20"></Setter> <Setter Property="Command" Value="{Binding NavigateCommand}"></Setter> <Setter Property="CommandParameter" Value="{Binding CommandParameter}"></Setter> </Style> </Menu.ItemContainerStyle> </Menu> </Grid> <ContentControl prism:RegionManager.RegionName="ContentRegion" /> </Grid> </Window>
菜单类中添加:
using Prism.Commands; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TestViews { public class Menus { /// <summary> /// 一级目录显示的文本 /// </summary> public string Content { get; set; } /// <summary> /// 二级菜单信息 /// </summary> public ObservableCollection<NavigateItemModel> PageList { get; set; } } /// <summary> /// 左侧二级导航项 /// </summary> public class NavigateItemModel { /// <summary> /// 界面显示的文本 /// </summary> public string Content { get; set; } /// <summary> /// 是否选中状态 /// </summary> public bool IsChecked { get; set; } /// <summary> /// 导航参数(要导航到的页面的名字) /// </summary> public string CommandParameter { get; set; } public int Size { get; set; } = 20; /// <summary> /// 导航命令 /// </summary> public DelegateCommand<string> NavigateCommand { get; set; } } }
提示: 某些时候,具有的集合包含其他集合。 HierarchicalDataTemplate 类专用于 HeaderedItemsControl 类型以显示此类数据这里填写该问题的具体解决方案:
<Menu Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="10" ItemsSource="{Binding MenusOne}"> <!--<Menu.Resources> <Style TargetType="Menu"> <Setter Property="FontSize" Value="20"/> </Style> </Menu.Resources>--> <Menu.ItemTemplate> <HierarchicalDataTemplate ItemsSource="{Binding PageList}"> <TextBlock Text="{Binding Content}"/> </HierarchicalDataTemplate> </Menu.ItemTemplate> <Menu.ItemContainerStyle> <Style TargetType="MenuItem"> <Setter Property="FontSize" Value="20"></Setter> <Setter Property="Command" Value="{Binding NavigateCommand}"></Setter> <Setter Property="CommandParameter" Value="{Binding CommandParameter}"></Setter> </Style> </Menu.ItemContainerStyle> </Menu>
private void Navigate(string navigatePath) { if (navigatePath != null) _regionManager.RequestNavigate("ContentRegion", navigatePath); } //可以将//if (navigatePath != null) // _regionManager.RequestNavigate("ContentRegion", navigatePath); 改为System.Windows.MessageBox.Show($"准备导航至 {navigatePath} 页面");去验证 private void Navigate(string navigatePath) { //if (navigatePath != null) // _regionManager.RequestNavigate("ContentRegion", navigatePath); System.Windows.MessageBox.Show($"准备导航至 {navigatePath} 页面"); } ``` // 总结:多看官方文档。有事找微软。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。