一、安装工具包
1、打开程序包管理器控制台
视图 > 其他窗口 > 程序包管理器控制台
2、安装程序包
打开管理NuGet程序包,搜索MaterialDesignThemes,进行安装
打开 视图 .> 其他窗口 > 程序包管理控制台
在程序包管理控制台中输入:
PM> Install-Package MaterialDesignThemes
PM> Install-Package ShowMeTheXAML.MSBuild
二、修改配置App.xaml
App.xaml文件
<Application x:Class="WpfApp3.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp3"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
三、在WPF窗口Xaml中引入命名控件
在MainWindow.xaml中的Window标签中添加
<Window [...]
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
[...] >
四、使用MaterialDesign控件
<Grid>
<materialDesign:PopupBox Closed="PopupBox_OnClosed"
IsEnabled="{Binding DataContext.ControlsEnabled, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"
Opened="PopupBox_OnOpened"
PlacementMode="BottomAndAlignCentres"
Style="{StaticResource MaterialDesignMultiFloatingActionPopupBox}"
ToolTip="PopupBox, Style MaterialDesignMultiFloatingActionPopupBox"
ToolTipService.Placement="Right">
<StackPanel>
<Button
Content="1"
Opacity="0.5"
ToolTip="One with custom opacity" />
<Button
Content="2"
ToolTip="Two" />
<Button
Content="3"
ToolTip="Three" />
</StackPanel>
</materialDesign:PopupBox>
</Grid>
此时代码报错,因为缺少一些引用。上述代码中使用了PopupBox控件,继续在App.xaml中添加
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.PopupBox.xaml" />
引用的程序集不全,有一些控件需要单独在App.xaml中引用一下程序集。