当前位置:   article > 正文

WPF实战项目二:设计首页导航条_wpf顶部导航

wpf顶部导航

 通过MaterialDesignThemes的github地址,下载代码,将导航条代码复制到MainWindow.xaml中

添加代码到<window..>

  1. xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
  2. TextElement.Foreground="{DynamicResource MaterialDesignBody}"
  3. TextElement.FontWeight="Regular"
  4. TextElement.FontSize="13"
  5. TextOptions.TextFormattingMode="Ideal"
  6. TextOptions.TextRenderingMode="Auto"
  7. Background="{DynamicResource MaterialDesignPaper}"
  8. FontFamily="{DynamicResource MaterialDesignFont}"

设置materialDesign的 x:Name="ColorZone"

添加三个按钮最小化、最大化、关闭,添加头像,设置头像为圆角显示,总体代码如下

  1. <Window x:Class="WPFProject.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
  7. xmlns:local="clr-namespace:WPFProject"
  8. mc:Ignorable="d"
  9. WindowStyle="None"
  10. WindowStartupLocation="CenterScreen"
  11. TextElement.Foreground="{DynamicResource MaterialDesignBody}"
  12. TextElement.FontWeight="Regular"
  13. TextElement.FontSize="13"
  14. TextOptions.TextFormattingMode="Ideal"
  15. TextOptions.TextRenderingMode="Auto"
  16. AllowsTransparency="True"
  17. Background="{DynamicResource MaterialDesignPaper}"
  18. FontFamily="{DynamicResource MaterialDesignFont}"
  19. Title="MainWindow"
  20. Width="800"
  21. Height="450">
  22. <materialDesign:DialogHost DialogTheme="Inherit"
  23. Identifier="RootDialog"
  24. SnackbarMessageQueue="{Binding ElementName=MainSnackbar, Path=MessageQueue}">
  25. <materialDesign:DrawerHost IsLeftDrawerOpen="{Binding ElementName=MenuToggleButton, Path=IsChecked}">
  26. <materialDesign:DrawerHost.LeftDrawerContent>
  27. <DockPanel MinWidth="220">
  28. </DockPanel>
  29. </materialDesign:DrawerHost.LeftDrawerContent>
  30. <DockPanel>
  31. <materialDesign:ColorZone Padding="16"
  32. x:Name="ColorZone"
  33. materialDesign:ElevationAssist.Elevation="Dp4"
  34. DockPanel.Dock="Top"
  35. Mode="PrimaryMid">
  36. <DockPanel LastChildFill="True">
  37. <StackPanel DockPanel.Dock="Right" Orientation="Horizontal">
  38. <Image Source="./Images/user.jpg" Height="25" Width="25">
  39. <Image.Clip><!--裁剪图片-->
  40. <EllipseGeometry Center="12.5,12.5" RadiusX="12.5" RadiusY="12.5"/>
  41. </Image.Clip>
  42. </Image>
  43. <!--设置水平往右排列-->
  44. <Button x:Name="BtnMin" Content="—" Style="{StaticResource MaterialDesignFlatMidBgButton}"/>
  45. <Button x:Name="BtnMax" Content="☐" Style="{StaticResource MaterialDesignFlatMidBgButton}"/>
  46. <Button x:Name="BtnClose" Content="✕" Style="{StaticResource MaterialDesignFlatMidBgButton}"/>
  47. </StackPanel>
  48. <StackPanel Orientation="Horizontal">
  49. <ToggleButton x:Name="MenuToggleButton"
  50. AutomationProperties.Name="HamburgerToggleButton"
  51. IsChecked="False"
  52. Style="{StaticResource MaterialDesignHamburgerToggleButton}" />
  53. <Button Margin="24,0,0,0"
  54. materialDesign:RippleAssist.Feedback="{Binding RelativeSource={RelativeSource Self}, Path=Foreground, Converter={StaticResource BrushRoundConverter}}"
  55. Command="{Binding MovePrevCommand}"
  56. Content="{materialDesign:PackIcon Kind=ArrowLeft,
  57. Size=24}"
  58. Foreground="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}"
  59. Style="{StaticResource MaterialDesignToolButton}"
  60. ToolTip="Previous Item" />
  61. <Button Margin="16,0,0,0"
  62. materialDesign:RippleAssist.Feedback="{Binding RelativeSource={RelativeSource Self}, Path=Foreground, Converter={StaticResource BrushRoundConverter}}"
  63. Command="{Binding MoveNextCommand}"
  64. Content="{materialDesign:PackIcon Kind=ArrowRight,
  65. Size=24}"
  66. Foreground="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}"
  67. Style="{StaticResource MaterialDesignToolButton}"
  68. ToolTip="Next Item" />
  69. <TextBlock Margin="16,0"
  70. HorizontalAlignment="Center"
  71. VerticalAlignment="Center"
  72. AutomationProperties.Name="Material Design In XAML Toolkit"
  73. FontSize="22"
  74. Text="WPFProject" />
  75. </StackPanel>
  76. </DockPanel>
  77. </materialDesign:ColorZone>
  78. </DockPanel>
  79. </materialDesign:DrawerHost>
  80. </materialDesign:DialogHost>
  81. </Window>

在解决方案窗体下添加图片文件夹,添加头像图片 

设置三个按钮的点击事件和拖动导航栏、双击导航栏的点击事件

  1. public MainWindow()
  2. {
  3. InitializeComponent();
  4. BtnMin.Click += (s, e) =>
  5. {
  6. this.WindowState = WindowState.Minimized;
  7. };
  8. BtnMax.Click += (s, e) =>
  9. {
  10. if (this.WindowState == WindowState.Maximized)
  11. this.WindowState = WindowState.Normal;
  12. else
  13. this.WindowState = WindowState.Maximized;
  14. };
  15. BtnClose.Click += (s, e) =>
  16. {
  17. this.Close();
  18. };
  19. ColorZone.MouseMove += (s, e) =>
  20. {
  21. if (e.LeftButton == MouseButtonState.Pressed)
  22. this.DragMove();
  23. };
  24. ColorZone.MouseDoubleClick += (s, e) =>
  25. {
  26. if (this.WindowState == WindowState.Normal)
  27. this.WindowState = WindowState.Maximized;
  28. else
  29. this.WindowState = WindowState.Normal;
  30. };
  31. }

 界面显示为:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/97814
推荐阅读
相关标签
  

闽ICP备14008679号