Hand in hand use WPF open source UI framework MahApps.Metro

1. Create a new project

Use Visual Studio 2022 to create a new project, click as shown in the figure 创建新项目:

insert image description here

select WPF应用(.NET Framework):

insert image description here

Configure a new project, as shown in the figure:

insert image description here
Just write a few WPF default style controls:

insert image description here

2. Install MahApps.Metro

Click Tools->NuGet Package Manager->Manage the NuGet package of the solution, as shown in the figure:

insert image description here
Search for MahApps, click the first one, and install it:

insert image description here
Click 确认, as shown in the figure:

insert image description here
The installation is complete, as shown in the figure:

insert image description here

3. Modify the code in App.xaml

Modify the following code in App.xaml:

<Application x:Class="FMSH_NVM_WPF.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:FMSH_NVM_WPF"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
                <!-- Theme setting -->
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Themes/Light.Blue.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

as the picture shows:

insert image description here
At this point, regenerate and run the program, and the control style inside the form has changed, as shown in the figure:

insert image description here

4. Modify the code of MainWindow.xaml

Modify the following code in MainWindow.xaml:

<mah:MetroWindow x:Class="FMSH_NVM_WPF.MainWindow"
        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:mah="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:FMSH_NVM_WPF"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Button Content="Button" HorizontalAlignment="Left" Margin="226,67,0,0" VerticalAlignment="Top"/>
        <Button Content="Button" HorizontalAlignment="Left" Margin="454,86,0,0" VerticalAlignment="Top"/>
        <Calendar HorizontalAlignment="Left" Margin="136,149,0,0" VerticalAlignment="Top"/>
        <TabControl Margin="390,170,10,22">
            <TabItem Header="TabItem">
                <Grid Background="#FFE5E5E5"/>
            </TabItem>
            <TabItem Header="TabItem">
                <Grid Background="#FFE5E5E5"/>
            </TabItem>
        </TabControl>

    </Grid>
</mah:MetroWindow>

as the picture shows:

insert image description here
Open the file MainWindow.xaml.cs and modify the code as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MahApps.Metro.Controls;

namespace FMSH_NVM_WPF
{
    
    
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : MetroWindow
    {
    
    
        public MainWindow()
        {
    
    
            InitializeComponent();
        }
    }
}

as the picture shows:

insert image description here
After saving and recompiling and running, the style of the form has changed, as shown in the figure:

insert image description here
MahApps official website: https://mahapps.com/

I hope this article is helpful to everyone. If there is anything wrong with the above, please correct me.

Sharing determines the height, and learning widens the gap

Guess you like

Origin blog.csdn.net/qq_42078934/article/details/132049765