LoginSignup
2
4

More than 3 years have passed since last update.

WPF Tips - Material Designにする

Last updated at Posted at 2019-06-19

概要

WPFアプリを作成する際に、Material Designにしようと思うんですが、
毎回忘れるので、備忘録として記事を作成(笑)。

手順

WPFプロジェクトを作成し、ソリューションエクスプローラ上でプロジェクトを右クリック。
メニューから「NuGetパッケージの管理」を選択します。

MaterialDesign_001.png

次に、参照タブを選び、検索枠に「MaterialDesign」と入力。
検索結果から「MaterialDesignThemes」を選択。
右側に情報が表示されるので、「インストール」をクリック

MaterialDesign_002.png

以下のようなダイアログが表示されるので、OKをクリック。

MaterialDesign_003.png

インストールが完了したら、App.xmalを開きます。Application.Resourceタグの中に、以下を記述します。

App.xmal
        <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.lightgreen.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.amber.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>

MainWindow.xamlのWindowタグ内に以下のパラメータを追加します。

MainWindow.xaml
        xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
        TextElement.Foreground="{DynamicResource MaterialDesignBody}"
        Background="{DynamicResource MaterialDesignPaper}"
        FontFamily="{DynamicResource MaterialDesignFont}"

設定は以上で完了。
試しにMainWindow.xamlにボタンを配置してみます
以下のようなコードを書いてみました。

    <Grid>
        <StackPanel Margin="10,10,0,0" VerticalAlignment="Top" Orientation="Horizontal" >
            <Button x:Name="SettingButton"
                        Margin="10">
                <StackPanel Orientation="Horizontal">
                    <materialDesign:PackIcon Kind="Settings" />
                    <TextBlock Margin="8 0 0 0" VerticalAlignment="Center">
                            設定
                    </TextBlock>
                </StackPanel>
            </Button>
        </StackPanel>
    </Grid>

実行結果はこんな感じ

MaterialDesign_004.png

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4