如何在来自外部资源字典的资源字典中多次使用样式? [英] How to use a style several times in a resourcedictionary from an external resourcedictionary?

查看:39
本文介绍了如何在来自外部资源字典的资源字典中多次使用样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在资源字典中有一个按钮样式,现在我想在另一个资源中多次使用这种样式(对于多个按钮),但它只影响最后一个按钮.这里有什么问题?

I have a style for button inside a resourcedictionary, and now I want to use this style in another resource several times(for several buttons), but it just effects on the last button. What's the problem here?

这是我的风格:(CommonControlStyles.xaml)

this is my style:(CommonControlStyles.xaml)

 <ResourceDictionary .....>
 .
 .
 .
 <Style  TargetType="{x:Type Button}" x:Key="ListButtonsStyle">
    <Setter Property="MaxHeight" Value="35"/>
    <Setter Property="Width" Value="20"/>
    <Setter Property="Content">
        <Setter.Value>
            <Image Source="Images\up.png"/>
        </Setter.Value>
    </Setter>
</Style>
.
.
.
</ResourceDictionary>

现在我想在另一个资源字典(myResourcedictionary.Xaml)中使用这种样式

and now i want to use this style in another resource dictionary(myResourcedictionary.Xaml)

        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/...StyleResource;component/CommonControlStyles.xaml" />
        </ResourceDictionary.MergedDictionaries>

 <Button  x:Name="btn1"  Grid.Column="0" Style="{DynamicResource ListButtonsStyle}"/>
 <Button  x:Name="btn2"  Grid.Column="0" Style="{DynamicResource ListButtonsStyle}"/>
 <Button  x:Name="btn3" Grid.Column="0" Style="{DynamicResource ListButtonsStyle}"/>

但它只影响btn3!!

but it just effects on btn3!!

推荐答案

不是DynamicResource,应该是StaticResource.

对于可变资源(例如:系统颜色),您应该使用 DynamicResource.但是你的风格是不变的,StaticResource 应该是你的选择.

You should use DynamicResource for changeable resources (for example: system colors). But your style is constant and StaticResource should be your choice.

这篇关于如何在来自外部资源字典的资源字典中多次使用样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆