赞
踩
Common->Models、ViewModel、Views
Models 内新建类文件 MenuBars.cs
Views内新建XAML文件MainView.xaml
MenuBars.cs 添加代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Mytodo.Common.Models { public class MenuBar { private string icon; /// <summary> /// 图标字符串 /// </summary> public string Icon { get { return icon; } set { icon = value; } } private string title; public string Title { get { return title; } set { title = value; } } private string nameSpace; public string NameSpace { get { return nameSpace; } set { nameSpace = value; } } } }
MainWindow内容,移动到Mainview.xaml中,并做相应修改,并把对应的.cs文件复制过来
x:Class="Mytodo.Views.MainView"
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace Mytodo.Views { /// <summary> /// MainView.xaml 的交互逻辑 /// </summary> public partial class MainView : Window { public MainView() { InitializeComponent(); btnclo.Click += (s, e) => { this.Close(); }; btnmin.Click += (s, e) => { this.WindowState = WindowState.Minimized; }; btnmax.Click += (s, e) => { this.WindowState = WindowState.Maximized; }; ColorZone.MouseMove += (s, e) => { if (e.LeftButton == MouseButtonState.Pressed) this.DragMove(); }; ColorZone.MouseDoubleClick += (s, e) => { if (WindowState != WindowState.Normal) WindowState = WindowState.Normal; else WindowState = WindowState.Maximized; }; } } }
using Mytodo.Common.Models; using Prism.Mvvm; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Mytodo.ViewModels { public class MainViewModel:BindableBase { public MainViewModel() { MenuBars=new ObservableCollection<MenuBar>(); CreatMenuBar(); } private ObservableCollection<MenuBar> menuBars; public ObservableCollection<MenuBar> MenuBars { get { return menuBars; } set { menuBars = value; RaisePropertyChanged(); } } void CreatMenuBar() { MenuBars.Add(new MenuBar { Icon = "Home", NameSpace = "HomeView", Title = "首页" }); MenuBars.Add(new MenuBar { Icon = "FormatListChecks", NameSpace = "TodoView", Title = "待办事项" }); MenuBars.Add(new MenuBar { Icon = "Notebook", NameSpace = "NoteView", Title = "备忘录" }); MenuBars.Add(new MenuBar { Icon = "Cog", NameSpace = "SettingView", Title = "设置" }); } } }
注意
<materialDesign:PackIcon Kind="{Binding Icon}" />
<Window x:Class="Mytodo.Views.MainView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:Mytodo" xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:prism="http://prismlibrary.com/" Title="MainWindow" Width="800" Height="450" prism:ViewModelLocator.AutoWireViewModel="True" Background="{DynamicResource MaterialDesignPaper}" FontFamily="{materialDesign:MaterialDesignFont}" Style="{StaticResource MaterialDesignWindow}" TextElement.FontSize="14" TextElement.FontWeight="Medium" TextElement.Foreground="{DynamicResource MaterialDesignBody}" WindowStartupLocation="CenterScreen" WindowStyle="None" mc:Ignorable="d"> <Grid> <materialDesign:DialogHost DialogTheme="Inherit" Identifier="RootDialog" SnackbarMessageQueue="{Binding ElementName=MainSnackbar, Path=MessageQueue}"> <materialDesign:DrawerHost IsLeftDrawerOpen="{Binding ElementName=MenuToggleButton, Path=IsChecked}"> <materialDesign:DrawerHost.LeftDrawerContent> <DockPanel MinWidth="220"> <StackPanel Margin="0,20" HorizontalAlignment="Center" DockPanel.Dock="Top"> <Image Width="50" Height="50" Margin="16,0,0,0" HorizontalAlignment="Right" Source="../Images/user.jpg"> <Image.Clip> <EllipseGeometry Center="25,25" RadiusX="25" RadiusY="25" /> </Image.Clip> </Image> <TextBlock Margin="0,10" HorizontalAlignment="Center" Text="donggg" /> </StackPanel> <ListBox HorizontalContentAlignment="Stretch" ItemContainerStyle="{StaticResource MyListboxItemStyle}" ItemsSource="{Binding MenuBars}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <materialDesign:PackIcon Kind="{Binding Icon}" /> <TextBlock Margin="10,0" Text="{Binding Title}" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </DockPanel> </materialDesign:DrawerHost.LeftDrawerContent> <materialDesign:ColorZone Name="ColorZone" Padding="16" materialDesign:ElevationAssist.Elevation="Dp4" DockPanel.Dock="Top" Mode="PrimaryMid"> <DockPanel> <StackPanel DockPanel.Dock="Left" Orientation="Horizontal"> <ToggleButton x:Name="MenuToggleButton" AutomationProperties.Name="HamburgerToggleButton" IsChecked="False" Style="{StaticResource MaterialDesignHamburgerToggleButton}" /> <Button Margin="24,0,0,0" materialDesign:RippleAssist.Feedback="{Binding RelativeSource={RelativeSource Self}, Path=Foreground, Converter={StaticResource BrushRoundConverter}}" Command="{Binding MovePrevCommand}" Content="{materialDesign:PackIcon Kind=ArrowLeft, Size=24}" Foreground="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}" Style="{StaticResource MaterialDesignToolButton}" ToolTip="Previous Item" /> <Button Margin="16,0,0,0" materialDesign:RippleAssist.Feedback="{Binding RelativeSource={RelativeSource Self}, Path=Foreground, Converter={StaticResource BrushRoundConverter}}" Command="{Binding MoveNextCommand}" Content="{materialDesign:PackIcon Kind=ArrowRight, Size=24}" Foreground="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}" Style="{StaticResource MaterialDesignToolButton}" ToolTip="Next Item" /> <TextBlock Margin="16,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Center" AutomationProperties.Name="Material Design In XAML Toolkit" FontSize="22" Text="笔记本" /> </StackPanel> <StackPanel HorizontalAlignment="Right" DockPanel.Dock="Right" Orientation="Horizontal"> <Image Width="30" Margin="16,0,0,0" HorizontalAlignment="Right" Source="../Images/user.jpg"> <Image.Clip> <EllipseGeometry Center="12.5,12.5" RadiusX="12.5" RadiusY="12.5" /> </Image.Clip> </Image> <Button x:Name="btnmin" Content="{materialDesign:PackIcon Kind=Minimize}" Style="{StaticResource MaterialDesignFlatAccentButton}" /> <Button x:Name="btnmax" Content="{materialDesign:PackIcon Kind=Maximize}" Style="{StaticResource MaterialDesignFlatAccentButton}" /> <Button x:Name="btnclo" Content="{materialDesign:PackIcon Kind=Close}" Style="{StaticResource MaterialDesignFlatAccentButton}" /> </StackPanel> </DockPanel> </materialDesign:ColorZone> </materialDesign:DrawerHost> </materialDesign:DialogHost> </Grid> </Window>
<materialDesign:DrawerHost.LeftDrawerContent> <DockPanel MinWidth="220"> <StackPanel Margin="0,20" HorizontalAlignment="Center" DockPanel.Dock="Top"> <Image Width="50" Height="50" Margin="16,0,0,0" HorizontalAlignment="Right" Source="../Images/user.jpg"> <Image.Clip> <EllipseGeometry Center="25,25" RadiusX="25" RadiusY="25" /> </Image.Clip> </Image> <TextBlock Margin="0,10" HorizontalAlignment="Center" Text="donggg" /> </StackPanel> <ListBox HorizontalContentAlignment="Stretch" ItemContainerStyle="{StaticResource MyListboxItemStyle}" ItemsSource="{Binding MenuBars}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <materialDesign:PackIcon Kind="{Binding Icon}" /> <TextBlock Margin="10,0" Text="{Binding Title}" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </DockPanel> </materialDesign:DrawerHost.LeftDrawerContent>
在App.xaml.cs中修改如下:
protected override Window CreateShell ()
{
return Container.Resolve<MainView> ();
}
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<materialDesign:BundledTheme
BaseTheme="Dark"
PrimaryColor="DeepPurple"
SecondaryColor="Lime" />
其中,BaseTheme=“Dark”
<Style x:Key="MyListboxItemStyle" TargetType="ListBoxItem"> <Setter Property="MinHeight" Value="40" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListBoxItem}"> <Grid> <Border x:Name="borderHeader" /> <Border x:Name="border" /> <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" /> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter TargetName="borderHeader" Property="BorderThickness" Value="4,0,0,0" /> <Setter TargetName="borderHeader" Property="BorderBrush" Value="{DynamicResource PrimaryHueLightBrush}" /> <Setter TargetName="border" Property="Background" Value="{DynamicResource PrimaryHueLightBrush}" /> <Setter TargetName="border" Property="Opacity" Value="0.2" /> </Trigger> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="border" Property="Background" Value="{DynamicResource PrimaryHueLightBrush}" /> <Setter TargetName="border" Property="Opacity" Value="0.2" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。