【问题标题】:How to use a style several times in a resourcedictionary from an external resourcedictionary?如何在外部资源字典中的资源字典中多次使用样式?
【发布时间】:2012-04-21 10:44:27
【问题描述】:

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

编辑: 这是我的风格:(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)

        <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 有影响!!

【问题讨论】:

    标签: wpf mvvm resourcedictionary


    【解决方案1】:

    不是DynamicResource,应该是StaticResource。

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

    【讨论】:

    • 你能发送链接到你的压缩项目吗?我无法想象它是如何不起作用的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多