![](/img/trans.png)
[英]WPF: Is it Possible to create Flyout on scrollable window using MahApps.Metro?
[英]How to create a base window using WPF and the MahApps framework?
我是WPF和XAML的新手,目前正在使用MahApps框架來獲取應用程序的Windows Metro主題。
我將按照本指南進行操作,以將Metro主題納入其中。
我的問題是如何創建具有MahApps主題的基本窗口,然后其他窗口可以從該基本窗口繼承,以便它們也可以獲取主題。
謝謝您的幫助!
這是一個簡短的如何創建基本MetroWindow
及其用法。
1)用您的基本窗口創建一個類(沒有任何XAML代碼)
using System.Windows;
using MahApps.Metro.Controls;
namespace MahAppsMetroSample
{
public class CustomBaseMetroWindow : MetroWindow
{
static CustomBaseMetroWindow()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomBaseMetroWindow), new FrameworkPropertyMetadata(typeof(CustomBaseMetroWindow)));
}
}
}
2)在您的解決方案中創建一個主題資源字典,將其稱為Generic.xaml
(僅是示例)
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mahAppsMetroSample="clr-namespace:MahAppsMetroSample"
xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Themes/MetroWindow.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="mahAppsMetroSample:CustomBaseMetroWindow" BasedOn="{StaticResource {x:Type controls:MetroWindow}}">
<Setter Property="TitleCharacterCasing" Value="Lower" />
<Setter Property="WindowTransitionsEnabled" Value="False" />
<Setter Property="WindowTitleBrush" Value="Brown" />
</Style>
</ResourceDictionary>
3)使用您的自定義窗口代替MetroWindow
using MahApps.Metro.Controls;
namespace MahAppsMetroSample
{
public partial class MainWindow : CustomBaseMetroWindow
{
public MainWindow()
{
InitializeComponent();
}
}
}
和
<mahAppsMetroSample:CustomBaseMetroWindow x:Class="MahAppsMetroSample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:mahAppsMetroSample="clr-namespace:MahAppsMetroSample"
Title="MainWindow">
<Grid>
</Grid>
</mahAppsMetroSample:CustomBaseMetroWindow>
您也可以在我的GitHub MahAppsMetroSample 代碼示例存儲庫中找到此示例。
希望這可以幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.