私は、WPFウィンドウにこのコードを持っている:WPFのスタイルは、実行時に適用していない(ただし、デザイナで動作します)
<Window.Resources>
<Style x:Key="MahappsStyle">
<Style.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Style.Resources>
</Style>
</Window.Resources>
アイデアは私のアプリケーションでは、単一の要素のための辞書で外部のスタイルを有効にすることです。
<mahapps:HamburgerMenu x:Name="hamburgerMenu" Style="{StaticResource MahappsStyle}"
DisplayMode="CompactOverlay">
</mahapps:HamburgerMenu>
をしかし、このアプローチは、デザイナで動作しているようだ、ではなく、実行時に:たとえば、「HamburgerMenu」という要素にスタイル「MahappsStyle」を適用することにより、動作するはずです。私は何が欠けていますか? MergedDictionariesを単一の要素に設定する他の方法はありますか?
UPDATE。これを行う方法が見つかりました。まず、以下の内容を使用してアプリケーションにMahapps.xamlを作成する必要があります。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mahapps="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:local="clr-namespace:Promt.Desktop">
<ResourceDictionary.MergedDictionaries >
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml"/>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
そしてすることによって、単一の要素に適用することが可能である:
<mahapps:HamburgerMenu>
<mahapps:HamburgerMenu.Resources>
<ResourceDictionary Source="pack://application:,,,/Promt.Desktop;component/Styles/Mahapps.xaml"/>
</mahapps:HamburgerMenu.Resources>
</mahapps:HamburgerMenu>
私はResourceDictionaryのが保持できないことを、本当にがっかりしていますx:キープロパティ。誰かが別のアプローチを知っているなら - 投稿してください。
UPDATE2。 Evkのさらに優れたソリューション(Laithの答えに基づく)。
「HamburgerMenu」からスタイル 'tag'を削除し、app.xamlファイル –
のスタイルファイルをマージします。@DarshanPatelは私が望むものではありません。この場合、スタイルはボタン、チェックボックスなどのすべてのアプリケーション要素に適用されます。 。それは避けるべき方法のポイントです –