从wpf中的单独文件加载控件样式

kcugc4gi  于 2022-11-18  发布在  其他
关注(0)|答案(5)|浏览(132)

我在Windows中添加了以下样式。资源

<Window.Resources>
    ...
    <!--A Style that extends the previous TextBlock Style-->
    <!--This is a "named style" with an x:Key of TitleText-->
    <Style BasedOn="{StaticResource {x:Type TextBlock}}"
       TargetType="TextBlock"
       x:Key="TitleText">
        <Setter Property="FontSize" Value="26"/>
        <Setter Property="Foreground">
            <Setter.Value>
                <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                    <LinearGradientBrush.GradientStops>
                        <GradientStop Offset="0.0" Color="#90DDDD" />
                        <GradientStop Offset="1.0" Color="#5BFFFF" />
                    </LinearGradientBrush.GradientStops>
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
    </Style> 
    ...
</Window.Resources>

我的xaml代码中有很多这样的样式,我想将每个组件样式保存到一个额外的文件(而不是外部文件)中..例如,与TextBlocks相关的所有样式都应该在名为TextBlockStyles.xaml的文件中
在wpf中我将如何做这件事?
如何链接项目中的样式?
先谢了

eiee3dmh

eiee3dmh1#

使用合并的资源字典
在您的app.xaml中,您可以使用

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary 
                Source="/Your.Assembly.Name;component/TextBlockStyles.xaml"/>
            ... other dictionaries here
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

或直接添加到UserControl中

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary 
                Source="/Your.Assembly.Name;component/TextBlockStyles.xaml"/>
            ... other dictionaries here
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

如果档案位于相同的组件和项目的根目录中,您可以将Source="..."缩短为Source="TextBlockStyles.xaml",或者如果您将资源字典放在文件夹Styles中,则可以缩短为Source="Styles\TextBlockStyles.xaml"

1bqhqjot

1bqhqjot2#

用例:您有一个名为MyView.xaml的用户控件,其中包含一个按钮。您希望使用外部XAML文件设置按钮的样式。
MyView.xaml中:

<User Control ...namespaces...>
    <UserControl.Resources>
        <ResourceDictionary>
            ...converters...

            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="MyButton.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>

     ...the rest of the control...
</UserControl>

MyButton.xaml中:

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

    <Style x:Key="FooButton" TargetType="{x:Type Button}">
        <Setter Property="Background" Value="Pink" />
    </Style>
</ResourceDictionary>

回到MyView.xaml(“其余控件”):

<Button Style="{StaticResource FooButton}">
    Hello World
</Button>
06odsfpq

06odsfpq3#

解决方案资源管理器中右键单击项目选择添加然后单击资源字典...选择名称并添加到项目中打开App.xaml将此代码添加到Application标记

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="YourStyle.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

在YourStyle.xaml中:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:APPNAME">
    <Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="Background" Value="Pink" />
    </Style>
</ResourceDictionary>
lx0bsm1f

lx0bsm1f4#

你正在寻找动态资源。最好的方法是在resources. application或者控制页面上加载和标记字典。这里有一个很好的例子。
http://blogs.msdn.com/b/wpfsdk/archive/2007/06/08/defining-and-using-shared-resources-in-a-custom-control-library.aspx

<ResourceDictionary>

  <ResourceDictionary.MergedDictionaries>

    <ResourceDictionary Source="Dictionary1.xaml"/>

  </ResourceDictionary.MergedDictionaries>

</ResourceDictionary>

添加(Smyresourcedictionary);

cwxwcias

cwxwcias5#

只需转到您的窗口(例如:MaindWindow.xaml),其中您希望包含外部文件中的资源,并使用MergedDictionaries原则引用该文件:

<Window x:Class="UseMergedResource.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow"
        Height="600"
        Width="600">
  <Window.Resources>

    <!-- DECLARING MERGED DICTIONARY --> 
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source='Merged/BrushResources.xaml' />
      </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    <!----------------------------------> 

  </Window.Resources>

  <StackPanel>
    <Rectangle Width='200'
               Height='100'
               Fill='{StaticResource PrimaryBrush}' /> <!-- USAGE HERE -->
  </StackPanel>
</Window>

从上面的Merged/BrushResources.xaml是资源文件的位置,它位于名为Merged的文件夹下。
现在,如果您想知道外部文件中的声明语法应该是什么,请检查以下内容:

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

    <!-- Location for application brushes -->
    <SolidColorBrush x:Key='BorderBrush'
                                     Color='Orange' />
    <SolidColorBrush x:Key='HighLightBrush'
                                     Color='LightBlue' />
    <SolidColorBrush x:Key='PrimaryBrush'
                                     Color='Pink' />
    <SolidColorBrush x:Key='AccentBrush'
                                     Color='Yellow' />

</ResourceDictionary>

如果你想让资源在所有应用程序中可用(在所有窗口中可见),那么在App.xaml资源部分声明。

相关问题