【问题标题】:wpf merge multiple styles into one stylewpf 将多种样式合并为一种样式
【发布时间】:2013-01-25 15:39:45
【问题描述】:

我有一个由列表视图组成的 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 个用户控件,它们之间唯一不同的是 &lt;UserControl.Resources&gt; 中的样式。不必仅仅因为我需要不同的外观和感觉就必须创建具有相同功能的多个控件。我现在要做的是将&lt;UserControl.Resources&gt; 中的所有样式合并为一种样式。如果我设法将所有这些样式归为一个,我将能够删除 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

【问题讨论】:

  • 我还发现你可以使用类似这样的代码:this.targetUserControl.Resources.Source = new Uri("common2.xaml",UriKind.Relative); 来改变它

标签: wpf resources styles


【解决方案1】:

将资源字典添加到您的应用程序中,名为 e.g. common.xaml

把你常用的样式放在那里

然后你可以重复使用它:

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

【讨论】:

  • +1 iltzortz 感谢您的帮助!它可以工作,但我不知道如何动态更改资源。看看我现在正在进行的编辑。
【解决方案2】:

您可以创建 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!" />

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

【讨论】:

  • 我也没有反对尝试这个
  • @TonoNam 你测试过这个吗?
  • 另一个有效,所以我没有时间尝试。我不知道为什么有人反对。投反对票的人应该说出原因。感谢我的帮助 +1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-27
  • 2013-08-15
  • 2014-08-06
  • 2012-04-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多