【问题标题】:WPF Style based on ResourceDictionary in App.xaml [duplicate]基于 App.xaml 中 ResourceDictionary 的 WPF 样式 [重复]
【发布时间】:2017-06-01 21:34:22
【问题描述】:

我在App.xaml 中使用ResourceDictionary 中定义的样式预设:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/FirstFloor.ModernUI;component/Assets/ModernUI.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

我现在想要的是能够在 另一个 xaml 中设置一个新样式,它不应该覆盖我的 App.xaml 的设置。

这应该看起来像这样:

<Grid.Resources>
    <Style TargetType="Button" BasedOn=".....">
        <Setter Property="Margin" Value="0,0,5,0"></Setter>
    </Style>
</Grid.Resources>

问题是我无法弄清楚我需要在 BasedOn 元素中写什么。

有人能告诉我一个简单的方法吗?

【问题讨论】:

    标签: c# wpf xaml resourcedictionary


    【解决方案1】:

    如果你想将你的风格基于默认的Button 风格,试试这个:

    <Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
    ...
    

    【讨论】:

    • 默认样式总是我在新样式之上设置的样式吗?顺便说一句,它工作正常
    • 默认样式在这种情况下是在您定义上述样式之前应用于范围内的按钮的样式,即您在 App.xaml 中合并的 ResourceDictionary 中定义的样式。您将新样式基于此。
    • 好吧,没想到会这么简单。是的,我不能接受,因为我之前有 5 分钟的冷却时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    相关资源
    最近更新 更多