WPF样式不在运行时应用(但在设计器中有效)

时间:2017-04-11 06:32:20

标签: c# wpf xaml

我在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>

我的想法是在我的应用程序中为单个元素启用字典中的外部样式。例如,它应该通过应用样式&#34; MahappsStyle&#34;称为&#34; HamburgerMenu&#34;的元素:

<mahapps:HamburgerMenu x:Name="hamburgerMenu" Style="{StaticResource MahappsStyle}"
                    DisplayMode="CompactOverlay">
</mahapps:HamburgerMenu>

但这种方法似乎只适用于设计人员,但不适用于运行时。我错过了什么?有没有其他方法可以将MergedDictionaries设置为单个元素?

更新。找到了这样做的方法。首先需要在应用程序中使用以下内容创建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:key属性。如果有人知道另一种方法 - 请发布。

UPDATE2。来自Evk的更好的解决方案(根据Laith的回答)。

2 个答案:

答案 0 :(得分:1)

你能检查一下是否有效:

<Window.Resources>
    <ResourceDictionary x:Key="MahappsResources">
        <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>
</Window.Resources>

并且您的控件可以使用键引用它:

<mahapps:HamburgerMenu Resources="{StaticResource MahappsResources}" />

答案 1 :(得分:0)

Laith的答案很接近,但并不完全存在,你需要这样做:

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary x:Key="MahappsResources">
            <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>
    </ResourceDictionary>
</Window.Resources>

然后你确实可以用钥匙来引用:

<mahapps:HamburgerMenu Resources="{StaticResource MahappsResources}" />

您需要再添加一个ResourceDictionary定义,否则它会将您的MahappsResources视为Window.Resources(因此类似于Window.Resources = new ResourceDictionary() ...)并在其上设置关键字确实毫无意义。当您再添加一个ResourceDictionary时 - 现在您确实使用给定密钥将MahappsResources添加到Window.Resources listaOis.add(new ObjectInputStream(s.getInputStream())); 字典,因此可以通过该密钥引用它。