2

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

  <ListView Style={DynamicResource style1} ...

目前,如果我这样做

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

这没用。


编辑

感谢 iltzortz 的回答,我现在有了:

字典1.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>

字典2.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>

我的用户控件:

<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

4

2 回答 2

1

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

在此处输入图像描述

把你常用的样式放在那里

然后你可以重用它:

 <UserControl.Resources>
    <ResourceDictionary Source="common.xaml"/>
 </UserControl.Resources>
于 2013-01-25T15:57:49.800 回答
0

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

例子:

字典1.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>

字典2.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!" />

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

于 2013-01-25T15:58:36.573 回答