0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

MaterialDesignThemesとMahApps.Metroを同時に使用する場合

Last updated at Posted at 2024-03-27

インストール

MaterialDesignThemes.MahApps
MaterialDesignColors
MahApps.Metro
上記の3つのライブラリをインストールする。
MaterialDesignThemesとMahApps.Metroを競合無く導入するには「MaterialDesignThemes」ではなく「MaterialDesignThemes.MahApps」をインストールする。

App.xamlの編集

App.xaml
<Application
    x:Class="MaterialDesignThemesWithMahApps.Metro.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+   xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
    xmlns:local="clr-namespace:MaterialDesignThemesWithMahApps.Metro"
    StartupUri="MainWindow.xaml">
    <Application.Resources>
+       <ResourceDictionary>
+           <ResourceDictionary.MergedDictionaries>
+                <!-- Theme -->
+               <materialDesign:MahAppsBundledTheme
+                   BaseTheme="Light"
+                   PrimaryColor="Green"
+                   SecondaryColor="LightGreen" />

+               <!-- MahApps -->
+               <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
+               <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />

+               <!-- Material Design -->
+               <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesign3.Defaults.xaml" />

+               <!-- Material Design: MahApps Compatibility -->
+               <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.MahApps;component/Themes/MaterialDesignTheme.MahApps.Defaults.xaml" />
+           </ResourceDictionary.MergedDictionaries>
+       </ResourceDictionary>
    </Application.Resources>
</Application>

MainWindow.xamlの編集

WindowからMetroWindowに変更する。

MainWindow.xaml
-<Window
+<mah:MetroWindow
    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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:MaterialDesignThemesWithMahApps.Metro"
+   xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
+   xmlns:materialDesignMahApps="http://materialdesigninxaml.net/winfx/xaml/themes"
    x:Class="MaterialDesignThemesWithMahApps.Metro.MainWindow"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
    <Grid>
        <materialDesignMahApps:Card Margin="20">
            <StackPanel>
                <TextBlock Text="MaterialDesignThemes with MahApps.Metro" Margin="20"/>
                <Button Content="Button" Margin="20 0 20 20"/>
            </StackPanel>
        </materialDesignMahApps:Card>
    </Grid>
-</Window>
+</mah:MetroWindow>

MainWindow.xaml.csの編集

Windowの継承をMetroWindowの継承に変更する。

MainWindow.xaml.cs
using MahApps.Metro.Controls;

namespace MaterialDesignThemesWithMahApps.Metro;

public partial class MainWindow : MetroWindow
{
    public MainWindow()
    {
        InitializeComponent();
    }
}

MetroWindowを使用しない場合

App.xamlを設定後、MainWindowにStyleを追加する。

MainWindow.xaml
<Window
    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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:MaterialDesignThemesWithMahApps.Metro"
    xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
    xmlns:materialDesignMahApps="http://materialdesigninxaml.net/winfx/xaml/themes"
    x:Class="MaterialDesignThemesWithMahApps.Metro.MainWindow"
    mc:Ignorable="d"
+   Style="{StaticResource MaterialDesignWindow}"
    Title="MainWindow" Height="450" Width="800">
    <Grid>
        <materialDesignMahApps:Card Margin="20">
            <StackPanel>
                <TextBlock Text="MaterialDesignThemes with MahApps.Metro" Margin="20"/>
                <Button Content="Button" Margin="20 0 20 20"/>
            </StackPanel>
        </materialDesignMahApps:Card>
    </Grid>
</Window>

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?