当前位置:   article > 正文

WPF基础之界面设计(MaterialDesignThemes)_wpf materialdesign

wpf materialdesign

界面设计源码地址:https://download.csdn.net/download/yigu4011/88011184

 

1、Nuget安装MaterialDesignThemes框架,安装LiveCharts、LiveCharts.Wpf(用来添加曲线,也可以下载LiveCharts.dll、LiveCharts.Wpf.dll,然后引用DLL)

 2、打开阿里巴巴素材库,下载素材,解压素材,将素材文件iconfont.ttf添加到项目

3、App.xaml中添加资源代码:

  1. <Application x:Class="WpfApp1.App"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:local="clr-namespace:WpfApp1"
  5. StartupUri="MainWindow.xaml">
  6. <Application.Resources>
  7. <ResourceDictionary>
  8. <ResourceDictionary.MergedDictionaries>
  9. <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
  10. <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
  11. <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
  12. <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
  13. </ResourceDictionary.MergedDictionaries>
  14. </ResourceDictionary>
  15. </Application.Resources>
  16. </Application>

 4、在MainWindow.xaml添加MaterialDesignThemes、LiveCharts、LiveCharts.Wpf的引用

  1. xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
  2. xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"

5、左边菜单栏关键性代码如下:(全部源码可去上方CSDN代码地址自由下载)

  1. <StackPanel Grid.Row="1">
  2. <Button
  3. Width="250"
  4. Height="50"
  5. Margin="15,30"
  6. HorizontalContentAlignment="Left"
  7. materialDesign:ShadowAssist.ShadowDepth="Depth3"
  8. Background="#9C27B3"
  9. BorderBrush="#9C27B3">
  10. <StackPanel Orientation="Horizontal">
  11. <!-- 图标 -->
  12. <TextBlock
  13. FontFamily="/#iconfont"
  14. FontSize="26"
  15. Foreground="White"
  16. Text="&#xf0070;" />
  17. <!-- 文字 -->
  18. <TextBlock
  19. Margin="15,0,0,0"
  20. VerticalAlignment="Center"
  21. Foreground="White"
  22. Text="Surface" />
  23. </StackPanel>
  24. </Button>
  25. <Button
  26. Width="250"
  27. Height="50"
  28. Margin="15,30"
  29. HorizontalContentAlignment="Left"
  30. materialDesign:ShadowAssist.ShadowDepth="Depth3"
  31. Background="Transparent"
  32. BorderBrush="Transparent">
  33. <StackPanel Orientation="Horizontal">
  34. <!-- 图标 -->
  35. <TextBlock
  36. FontFamily="/#iconfont"
  37. FontSize="26"
  38. Foreground="#848484"
  39. Text="&#xe69a;" />
  40. <!-- 文字 -->
  41. <TextBlock
  42. Margin="15,0,0,0"
  43. VerticalAlignment="Center"
  44. Foreground="#848484"
  45. Text="Bing" />
  46. </StackPanel>
  47. </Button>
  48. <Button
  49. Width="250"
  50. Height="50"
  51. Margin="15,30"
  52. HorizontalContentAlignment="Left"
  53. materialDesign:ShadowAssist.ShadowDepth="Depth3"
  54. Background="Transparent"
  55. BorderBrush="Transparent">
  56. <StackPanel Orientation="Horizontal">
  57. <!-- 图标 -->
  58. <TextBlock
  59. FontFamily="/#iconfont"
  60. FontSize="26"
  61. Foreground="#848484"
  62. Text="&#xe86f;" />
  63. <!-- 文字 -->
  64. <TextBlock
  65. Margin="15,0,0,0"
  66. VerticalAlignment="Center"
  67. Foreground="#848484"
  68. Text="Windows" />
  69. </StackPanel>
  70. </Button>
  71. <Button
  72. Width="250"
  73. Height="50"
  74. Margin="15,30"
  75. HorizontalContentAlignment="Left"
  76. materialDesign:ShadowAssist.ShadowDepth="Depth3"
  77. Background="Transparent"
  78. BorderBrush="Transparent">
  79. <StackPanel Orientation="Horizontal">
  80. <!-- 图标 -->
  81. <TextBlock
  82. FontFamily="/#iconfont"
  83. FontSize="26"
  84. Foreground="#848484"
  85. Text="&#xe8e8;" />
  86. <!-- 文字 -->
  87. <TextBlock
  88. Margin="15,0,0,0"
  89. VerticalAlignment="Center"
  90. Foreground="#848484"
  91. Text="Xbox" />
  92. </StackPanel>
  93. </Button>
  94. <Button
  95. Width="250"
  96. Height="50"
  97. Margin="15,30"
  98. HorizontalContentAlignment="Left"
  99. materialDesign:ShadowAssist.ShadowDepth="Depth3"
  100. Background="Transparent"
  101. BorderBrush="Transparent">
  102. <StackPanel Orientation="Horizontal">
  103. <!-- 图标 -->
  104. <TextBlock
  105. FontFamily="/#iconfont"
  106. FontSize="26"
  107. Foreground="#848484"
  108. Text="&#xe8ce;" />
  109. <!-- 文字 -->
  110. <TextBlock
  111. Margin="15,0,0,0"
  112. VerticalAlignment="Center"
  113. Foreground="#848484"
  114. Text="Hololens" />
  115. </StackPanel>
  116. </Button>
  117. <Button
  118. Width="250"
  119. Height="50"
  120. Margin="15,30"
  121. HorizontalContentAlignment="Left"
  122. materialDesign:ShadowAssist.ShadowDepth="Depth3"
  123. Background="Transparent"
  124. BorderBrush="Transparent">
  125. <StackPanel Orientation="Horizontal">
  126. <!-- 图标 -->
  127. <TextBlock
  128. FontFamily="/#iconfont"
  129. FontSize="26"
  130. Foreground="#848484"
  131. Text="&#xec16;" />
  132. <!-- 文字 -->
  133. <TextBlock
  134. Margin="15,0,0,0"
  135. VerticalAlignment="Center"
  136. Foreground="#848484"
  137. Text="Azure" />
  138. </StackPanel>
  139. </Button>
  140. </StackPanel>

 6、右边显示关键代码如下:

  1. <UniformGrid Grid.Row="2" Columns="3">
  2. <Border
  3. Margin="20,20,20,20"
  4. Background="White"
  5. CornerRadius="5">
  6. <Grid>
  7. <Grid.RowDefinitions>
  8. <RowDefinition Height="1.7*" />
  9. <RowDefinition />
  10. </Grid.RowDefinitions>
  11. <Grid Margin="20,-20,20,10" Panel.ZIndex="1">
  12. <Grid.Effect>
  13. <DropShadowEffect
  14. BlurRadius="15"
  15. Direction="-90"
  16. Opacity=".2"
  17. RenderingBias="Quality"
  18. ShadowDepth="1" />
  19. </Grid.Effect>
  20. <Grid.Resources>
  21. <Style TargetType="lvc:ColumnSeries">
  22. <Setter Property="StrokeThickness" Value="0" />
  23. <Setter Property="Stroke" Value="White" />
  24. <Setter Property="Fill" Value="White" />
  25. <Setter Property="MaxColumnWidth" Value="5" />
  26. </Style>
  27. <Style TargetType="lvc:Axis">
  28. <Setter Property="FontSize" Value="12" />
  29. <Setter Property="Foreground" Value="#64FFFFFF" />
  30. <Style.Triggers>
  31. <Trigger Property="AxisOrientation" Value="Y">
  32. <Setter Property="IsMerged" Value="True" />
  33. <Setter Property="MaxValue" Value="10" />
  34. </Trigger>
  35. </Style.Triggers>
  36. </Style>
  37. <Style TargetType="lvc:Separator">
  38. <Setter Property="StrokeThickness" Value="1" />
  39. <Setter Property="Stroke" Value="#4BFFFFFF" />
  40. <Style.Triggers>
  41. <Trigger Property="AxisOrientation" Value="X">
  42. <Setter Property="IsEnabled" Value="False" />
  43. <Setter Property="Step" Value="1" />
  44. </Trigger>
  45. </Style.Triggers>
  46. </Style>
  47. </Grid.Resources>
  48. <Grid.RowDefinitions>
  49. <RowDefinition Height="Auto" />
  50. <RowDefinition Height="Auto" />
  51. <RowDefinition Height=".50*" />
  52. </Grid.RowDefinitions>
  53. <Border
  54. x:Name="Border1"
  55. Grid.Row="0"
  56. Grid.RowSpan="4"
  57. Background="White"
  58. CornerRadius="5" />
  59. <Border Grid.RowSpan="3" Background="#51AB54" />
  60. <lvc:CartesianChart
  61. Grid.Row="2"
  62. Margin="10,0,10,20"
  63. DataTooltip="{x:Null}"
  64. Hoverable="False">
  65. <lvc:CartesianChart.Series>
  66. <lvc:ColumnSeries Values="5,9,8,6,1,5,7,3,6,3" />
  67. </lvc:CartesianChart.Series>
  68. </lvc:CartesianChart>
  69. </Grid>
  70. <Grid Grid.Row="1">
  71. <Grid.ColumnDefinitions>
  72. <ColumnDefinition Width="101*" />
  73. <ColumnDefinition Width="399*" />
  74. </Grid.ColumnDefinitions>
  75. <Grid.RowDefinitions>
  76. <RowDefinition Height="1.5*" />
  77. <RowDefinition />
  78. </Grid.RowDefinitions>
  79. <StackPanel
  80. Grid.ColumnSpan="2"
  81. VerticalAlignment="Bottom"
  82. Orientation="Vertical">
  83. <TextBlock
  84. Margin="20,0"
  85. FontSize="25"
  86. Foreground="#959595"
  87. Text="XBOX" />
  88. <TextBlock
  89. Margin="20,0"
  90. FontSize="13"
  91. Foreground="#C8C8C8"
  92. Text="Get More Data" />
  93. <Border
  94. Margin="30,5,30,0"
  95. VerticalAlignment="Bottom"
  96. BorderBrush="DarkGray"
  97. BorderThickness="0,0,0,1" />
  98. </StackPanel>
  99. <TextBlock
  100. Grid.Row="1"
  101. Grid.ColumnSpan="2"
  102. Margin="20,0,20,0"
  103. VerticalAlignment="Center"
  104. FontSize="13"
  105. Foreground="#C8C8C8"
  106. Text="updated 4 minutes ago" />
  107. </Grid>
  108. </Grid>
  109. </Border>
  110. <Border
  111. Margin="20,20,20,20"
  112. Background="White"
  113. CornerRadius="5">
  114. <Grid>
  115. <Grid.RowDefinitions>
  116. <RowDefinition Height="1.7*" />
  117. <RowDefinition />
  118. </Grid.RowDefinitions>
  119. <Grid Margin="20,-20,20,10" Panel.ZIndex="1">
  120. <Grid.Effect>
  121. <DropShadowEffect
  122. BlurRadius="15"
  123. Direction="-90"
  124. Opacity=".2"
  125. RenderingBias="Quality"
  126. ShadowDepth="1" />
  127. </Grid.Effect>
  128. <Grid.Resources>
  129. <Style TargetType="lvc:ColumnSeries">
  130. <Setter Property="StrokeThickness" Value="0" />
  131. <Setter Property="Stroke" Value="White" />
  132. <Setter Property="Fill" Value="White" />
  133. <Setter Property="MaxColumnWidth" Value="5" />
  134. </Style>
  135. <Style TargetType="lvc:Axis">
  136. <Setter Property="FontSize" Value="12" />
  137. <Setter Property="Foreground" Value="#64FFFFFF" />
  138. <Style.Triggers>
  139. <Trigger Property="AxisOrientation" Value="Y">
  140. <Setter Property="IsMerged" Value="True" />
  141. <Setter Property="MaxValue" Value="10" />
  142. </Trigger>
  143. </Style.Triggers>
  144. </Style>
  145. <Style TargetType="lvc:Separator">
  146. <Setter Property="StrokeThickness" Value="1" />
  147. <Setter Property="Stroke" Value="#4BFFFFFF" />
  148. <Style.Triggers>
  149. <Trigger Property="AxisOrientation" Value="X">
  150. <Setter Property="IsEnabled" Value="False" />
  151. <Setter Property="Step" Value="1" />
  152. </Trigger>
  153. </Style.Triggers>
  154. </Style>
  155. </Grid.Resources>
  156. <Grid.RowDefinitions>
  157. <RowDefinition Height="Auto" />
  158. <RowDefinition Height="Auto" />
  159. <RowDefinition Height=".50*" />
  160. </Grid.RowDefinitions>
  161. <Border
  162. Grid.Row="0"
  163. Grid.RowSpan="4"
  164. Background="White"
  165. CornerRadius="5" />
  166. <Border
  167. Grid.Row="0"
  168. Grid.RowSpan="3"
  169. Background="#FD950E" />
  170. <lvc:CartesianChart
  171. Grid.Row="2"
  172. Margin="10,0,10,20"
  173. DataTooltip="{x:Null}"
  174. Hoverable="False">
  175. <lvc:CartesianChart.Series>
  176. <lvc:LineSeries Stroke="White" Values="5,9,8,6,1,5,7,3,6,3" />
  177. </lvc:CartesianChart.Series>
  178. </lvc:CartesianChart>
  179. </Grid>
  180. <Grid Grid.Row="1">
  181. <Grid.RowDefinitions>
  182. <RowDefinition Height="1.5*" />
  183. <RowDefinition />
  184. </Grid.RowDefinitions>
  185. <StackPanel VerticalAlignment="Bottom" Orientation="Vertical">
  186. <TextBlock
  187. Margin="20,0"
  188. FontSize="25"
  189. Foreground="#959595"
  190. Text="Office" />
  191. <TextBlock
  192. Margin="20,0"
  193. FontSize="13"
  194. Foreground="#C8C8C8"
  195. Text="Get More Data" />
  196. <Border
  197. Margin="30,5,30,0"
  198. VerticalAlignment="Bottom"
  199. BorderBrush="DarkGray"
  200. BorderThickness="0,0,0,1" />
  201. </StackPanel>
  202. <TextBlock
  203. Grid.Row="1"
  204. Margin="20,0"
  205. VerticalAlignment="Center"
  206. FontSize="13"
  207. Foreground="#C8C8C8"
  208. Text="compaign sent 2 days ago" />
  209. </Grid>
  210. </Grid>
  211. </Border>
  212. <Border
  213. Margin="20,20,20,20"
  214. Background="White"
  215. CornerRadius="5">
  216. <Grid>
  217. <Grid.RowDefinitions>
  218. <RowDefinition Height="1.7*" />
  219. <RowDefinition />
  220. </Grid.RowDefinitions>
  221. <Grid Margin="20,-20,20,10" Panel.ZIndex="1">
  222. <Grid.Effect>
  223. <DropShadowEffect
  224. BlurRadius="15"
  225. Direction="-90"
  226. Opacity=".2"
  227. RenderingBias="Quality"
  228. ShadowDepth="1" />
  229. </Grid.Effect>
  230. <Grid.Resources>
  231. <Style TargetType="lvc:ColumnSeries">
  232. <Setter Property="StrokeThickness" Value="0" />
  233. <Setter Property="Stroke" Value="White" />
  234. <Setter Property="Fill" Value="White" />
  235. <Setter Property="MaxColumnWidth" Value="5" />
  236. </Style>
  237. <Style TargetType="lvc:Axis">
  238. <Setter Property="FontSize" Value="12" />
  239. <Setter Property="Foreground" Value="#64FFFFFF" />
  240. <Style.Triggers>
  241. <Trigger Property="AxisOrientation" Value="Y">
  242. <Setter Property="IsMerged" Value="True" />
  243. <Setter Property="MaxValue" Value="10" />
  244. </Trigger>
  245. </Style.Triggers>
  246. </Style>
  247. <Style TargetType="lvc:Separator">
  248. <Setter Property="StrokeThickness" Value="1" />
  249. <Setter Property="Stroke" Value="#4BFFFFFF" />
  250. <Style.Triggers>
  251. <Trigger Property="AxisOrientation" Value="X">
  252. <Setter Property="IsEnabled" Value="False" />
  253. <Setter Property="Step" Value="1" />
  254. </Trigger>
  255. </Style.Triggers>
  256. </Style>
  257. </Grid.Resources>
  258. <Grid.RowDefinitions>
  259. <RowDefinition Height="Auto" />
  260. <RowDefinition Height="Auto" />
  261. <RowDefinition Height=".50*" />
  262. </Grid.RowDefinitions>
  263. <Border
  264. Grid.Row="0"
  265. Grid.RowSpan="4"
  266. Background="White"
  267. CornerRadius="5" />
  268. <Border
  269. Grid.Row="0"
  270. Grid.RowSpan="3"
  271. Background="#E94543" />
  272. <lvc:CartesianChart
  273. Grid.Row="2"
  274. Margin="0,0,0,0"
  275. DataTooltip="{x:Null}"
  276. Hoverable="True">
  277. <lvc:CartesianChart.Series>
  278. <lvc:LineSeries Stroke="White" Values="5,9,8,6,7,2,1,5,7,8," />
  279. </lvc:CartesianChart.Series>
  280. <lvc:CartesianChart.AxisX>
  281. <lvc:Axis MinValue="2" />
  282. </lvc:CartesianChart.AxisX>
  283. </lvc:CartesianChart>
  284. </Grid>
  285. <Grid Grid.Row="1">
  286. <Grid.RowDefinitions>
  287. <RowDefinition Height="1.5*" />
  288. <RowDefinition />
  289. </Grid.RowDefinitions>
  290. <StackPanel VerticalAlignment="Bottom" Orientation="Vertical">
  291. <TextBlock
  292. Margin="20,0"
  293. FontSize="25"
  294. Foreground="#959595"
  295. Text="Surface" />
  296. <TextBlock
  297. Margin="20,0"
  298. FontSize="13"
  299. Foreground="#C8C8C8"
  300. Text="Get More Data" />
  301. <Border
  302. Margin="30,5,30,0"
  303. VerticalAlignment="Bottom"
  304. BorderBrush="DarkGray"
  305. BorderThickness="0,0,0,1" />
  306. </StackPanel>
  307. <TextBlock
  308. Grid.Row="1"
  309. Margin="20,0"
  310. VerticalAlignment="Center"
  311. FontSize="13"
  312. Foreground="#C8C8C8"
  313. Text="compaign sent 2 days ago" />
  314. </Grid>
  315. </Grid>
  316. </Border>
  317. </UniformGrid>

6、关键事件代码如下:

  1. /// <summary>
  2. /// MainWindow.xaml 的交互逻辑
  3. /// </summary>
  4. public partial class MainWindow : Window
  5. {
  6. public MainWindow()
  7. {
  8. InitializeComponent();
  9. }
  10. Point _pressedPosition;
  11. bool _isDragMoved = false;
  12. private void Window_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
  13. {
  14. _pressedPosition = e.GetPosition(this);
  15. }
  16. private void Window_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
  17. {
  18. if (Mouse.LeftButton == MouseButtonState.Pressed && _pressedPosition != e.GetPosition(this))
  19. {
  20. _isDragMoved = true;
  21. DragMove();
  22. }
  23. }
  24. private void Window_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  25. {
  26. if (_isDragMoved)
  27. {
  28. _isDragMoved = false;
  29. e.Handled = true;
  30. }
  31. }
  32. private void btnMin_Click(object sender, RoutedEventArgs e)
  33. {
  34. this.WindowState = WindowState.Minimized;
  35. }
  36. private void btnMax_Click(object sender, RoutedEventArgs e)
  37. {
  38. if (this.WindowState == WindowState.Maximized)
  39. {
  40. this.WindowState = WindowState.Normal;
  41. }
  42. else
  43. {
  44. this.WindowState = WindowState.Maximized;
  45. }
  46. }
  47. private void btnClose_Click(object sender, RoutedEventArgs e)
  48. {
  49. this.Close();
  50. }
  51. }

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

闽ICP备14008679号