閱讀導航
本文背景程式碼實現本文參考原始碼1. 本文背景WPF中垂直導航選單大家應該都常用,本文介紹使用MVVM的方式怎麼繫結選單,真的很簡單。
2. 程式碼實現使用 .Net Core 3.1 建立名為 “MenuMVVM” 的WPF模板專案,新增兩個Nuget庫:MaterialDesignThemes和MaterialDesignColors。
解決方案目錄結構(這次失誤,未截圖,網站可以檢視):
2.1 引入MD控制元件樣式檔案【App.xaml】,在StartupUri中設定啟動的檢視【Views/MainView.xaml】,並在【Application.Resources】節點增加MD控制元件4個樣式檔案
<Application x:Class="MenuMVVM.App" xmlns="/file/2020/01/15/20200115222354_16588.jpg.aspx xmlns:x="/file/2020/01/15/20200115222355_16589.jpg.aspx StartupUri="Views/MainView.xaml"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Dark.xaml" /> <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" /> <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.Blue.xaml" /> <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.LightBlue.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources></Application>
2.2 Models兩個簡單的選單實體類:
2.2.1 選單新檔案資訊檔案【ItemCount.cs】,定義選單項右側的新檔案顯示個數及顯示背景色:
using System.Windows.Media;namespace MenuMVVM.Models{ public class ItemCount { public Brush Color { get; private set; } public int Value { get; private set; } public ItemCount(Brush color, int value) { Color = color; Value = value; } }}
2.2.2 選單項資訊
檔案【MenuItem.cs】,定義選單項展示的名稱、圖片、新檔案資訊:
using MaterialDesignThemes.Wpf;using System;namespace MenuMVVM.Models{ public class MenuItem { public String Name { get; private set; } public PackIconKind Icon { get; private set; } public ItemCount Count { get; private set; } public MenuItem(String name, PackIconKind icon, ItemCount count) { Name = name; Icon = icon; Count = count; } }}
其中選單項圖示使用MD控制元件自帶的字型圖示庫,通過列舉【PackIconKind】可以很方便的使用,該庫提供的字型圖示非常豐富,目前有4836個(列舉值有7883個), 下面是最後幾個:
//// 摘要:// List of available icons for use with MaterialDesignThemes.Wpf.PackIcon.//// 言論:// All icons sourced from Material Design Icons Font - /file/2020/01/15/20200115222356_16590.jpg - in accordance of https://github.com/Templarian/MaterialDesign/blob/master/license.txt.public enum PackIconKind{ . . . ZodiacPisces = 4832, HoroscopePisces = 4832, ZodiacSagittarius = 4833, HoroscopeSagittarius = 4833, ZodiacScorpio = 4834, HoroscopeScorpio = 4834, ZodiacTaurus = 4835, HoroscopeTaurus = 4835, ZodiacVirgo = 4836, HoroscopeVirgo = 4836}
2.3 ViewModels檔案【MainViewModel.cs】,只定義了簡單的幾個屬性:窗體展示Logo、選單繫結列表。屬性定義比較簡單,因為檢視MainView.xaml展示內容不多:
using MaterialDesignThemes.Wpf;using MenuMVVM.Models;using System.Collections.Generic;using System.Windows.Media;namespace MenuMVVM.ViewModels{ public class MainViewModel { public string Logo { get; set; } public List<MenuItem> LeftMenus { get; set; } public MainViewModel() { Logo = "/file/2020/01/15/20200115222408_16594.jpg.png"; LeftMenus = new List<MenuItem>(); LeftMenus.Add(new MenuItem("圖片", PackIconKind.Image, new ItemCount(Brushes.Black, 2))); LeftMenus.Add(new MenuItem("音樂", PackIconKind.Music, new ItemCount(Brushes.DarkBlue, 4))); LeftMenus.Add(new MenuItem("視訊", PackIconKind.Video, new ItemCount(Brushes.DarkGreen, 7))); LeftMenus.Add(new MenuItem("文件", PackIconKind.Folder, new ItemCount(Brushes.DarkOrange, 9))); } }}
2.4 Views檔案【MainView.xaml】作為唯一的檢視,只有31行佈局程式碼,顯示了一個Logo、選單列表:
<Window x:Class="MenuMVVM.Views.MainView" xmlns="/file/2020/01/15/20200115222354_16588.jpg.aspx xmlns:materialDesign="/file/2020/01/15/20200115222406_16592.jpg xmlns:d="/file/2020/01/15/20200115222407_16593.jpg.aspx xmlns:x="/file/2020/01/15/20200115222355_16589.jpg.aspx xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" Title="Dotnet9" Height="600" Width="1080" Background="#FF36235F" MouseLeftButtonDown="Window_MouseLeftButtonDown" WindowStyle="None" ResizeMode="NoResize" WindowStartupLocation="CenterScreen"> <Grid> <StackPanel Width="200" HorizontalAlignment="Left" Background="#FF472076"> <Grid Height="150" Background="White"> <Image Source="{Binding Logo}"/> </Grid> <ListView ItemsSource="{Binding LeftMenus}"> <ListView.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal" Height="30"> <materialDesign:PackIcon Kind="{Binding Path=Icon}" Width="20" Height="20" VerticalAlignment="Center"/> <TextBlock Text="{Binding Path=Name}" Margin="20 0" FontSize="15" VerticalAlignment="Center"/> <Grid VerticalAlignment="Center"> <Rectangle Width="30" Height="15" RadiusY="7.15" RadiusX="7.15" Fill="{Binding Path=Count.Color}" Stroke="White" StrokeThickness="0.7"/> <TextBlock Text="{Binding Path=Count.Value}" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="9"/> </Grid> </StackPanel> </DataTemplate> </ListView.ItemTemplate> </ListView> </StackPanel> </Grid></Window>
檔案【MainView.xaml.cs】作為檢視【MainView.xaml】的後臺,繫結ViewModel,並實現滑鼠左鍵拖動窗體功能:
using MenuMVVM.ViewModels;using System.Windows;namespace MenuMVVM.Views{ /// <summary> /// 演示主窗體,只用於簡單的繫結ListView控制元件 /// </summary> public partial class MainView : Window { public MainView() { this.DataContext = new MainViewModel(); InitializeComponent(); } private void Window_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e) { DragMove(); } }}
3.本文參考視訊一:C# WPF Design UI: Navigation Drawer Model View View Mode,配套原始碼:MenuMVVM。4.原始碼
文中程式碼已經全部給出,圖片使用站長網站外鏈,可直接Copy程式碼,按解決方案目錄組織程式碼檔案即可執行,另附原作者視訊及原始碼,見【3.本文參考】。
除非註明,文章均由 Dotnet9 整理髮布,歡迎轉載。轉載請註明本文地址:https://dotnet9.com/7339.html