wpf将多个样式合并为一种样式

时间:2013-01-25 15:39:45

标签: wpf resources styles

我有一个UserControl,它包含一个列表视图:

<UserControl
           ....

    <UserControl.Resources>

        <Style TargetType="Thumb">
            <!-- Style Content -->
        </Style>

        <Style  TargetType="GridViewColumnHeader">
            <!-- Style Content -->
        </Style>

        <Style  TargetType="{x:Type ScrollBar}">
            <!-- Style Content -->
        </Style>

        <Style  TargetType="{x:Type ScrollViewer}">
            <!-- Style Content -->
        </Style>

        <Style TargetType="{x:Type ListViewItem}">
            <!-- Style Content -->
        </Style>

    </UserControl.Resources>

    <ListView Name="ListView1" >
            <!-- ListViewContent -->
    </Style>
</UserControl>

我有3个userControl,其中唯一不同的是<UserControl.Resources>中的样式。它不需要创建具有相同功能的多个控件,因为我需要不同的外观。我现在要做的是将<UserControl.Resources>中的所有样式组合成一种样式。如果我设法将所有这些样式分组为一个,我将能够删除3个控件并将样式更改为:

  <ListView Style={DynamicResource style1} ...

目前如果我这样做

<UserControl.Resources>
     <Style x:Key="style1">
         <!-- Place all styles in here -->
     </Style>
</UserControl.Resources>

它不起作用。


修改

感谢iltzortz回答我现在有:

Dictionary1.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="Grid">        
        <Setter Property="Background" Value="Green"></Setter>
    </Style>

    <SolidColorBrush x:Key="Foo" Color="Red"></SolidColorBrush>

</ResourceDictionary>

Dictionary2.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="Grid">        
        <Setter Property="Background" Value="Black"></Setter>
    </Style>

    <SolidColorBrush x:Key="Foo" Color="Orange"></SolidColorBrush>

</ResourceDictionary>

的MyUserControl:

<UserControl x:Class="WpfApplication1.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="97" d:DesignWidth="91">

    <UserControl.Resources>
        <ResourceDictionary Source="Dictionary1.xaml" ></ResourceDictionary>
    </UserControl.Resources>

    <Grid >
        <Ellipse Fill="{DynamicResource Foo}" />
    </Grid>
</UserControl>

我动态更改资源词典:switching wpf resource dictionaries at runtime

2 个答案:

答案 0 :(得分:1)

将资源字典添加到您的应用程序,例如: common.xaml

enter image description here

并将您的常用样式放在那里

然后您可以将其重复使用:

 <UserControl.Resources>
    <ResourceDictionary Source="common.xaml"/>
 </UserControl.Resources>

答案 1 :(得分:0)

您可以创建3个资源字典并在运行时合并它们。在我的示例代码中,我使用了两个资源字典。

示例:

Dictionary1.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="btnStyle" TargetType="Button">
        <Setter Property="Margin" Value="0,10,0,0" />
    </Style>
</ResourceDictionary>

Dictionary2.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style x:Key="btnStyle" TargetType="Button">
        <Setter Property="Margin" Value="50,50,0,0" />
    </Style>

</ResourceDictionary>

在应用程序开始时,您可以在App.xaml文件中设置默认样式:

<Application x:Class="Example.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Dictionary1.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

如果要更改样式,可以合并资源词典:

ResourceDictionary dict = new ResourceDictionary();
dict.Source = new Uri("\\Dictionary2.xaml", UriKind.Relative);            
this.Resources.MergedDictionaries.Add(dict);

现在绑定看起来像这样:

<Button Style="{DynamicResource btnStyle}" Content="Click me!" />

现在,如果您调用代码来合并资源字典,则会自动更改按钮样式。