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

leb*_*ero 13 .net c# wpf

我在Windows.Resources中添加了以下样式

<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>
Run Code Online (Sandbox Code Playgroud)

我的xaml代码中有很多样式,我想将每个组件样式保存到一个额外的文件(而不是外部文件)..例如,与TextBlocks相关的所有样式都应该在一个名为的文件中 TextBlockStyles.xaml

我怎么能在wpf中这样做?

如何链接项目中的样式?

提前致谢

Phi*_*hil 24

您使用合并的资源字典

在你app.xaml你会使用

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary 
                Source="/Your.Assembly.Name;component/TextBlockStyles.xaml"/>
            ... other dictionaries here
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)

或直接进入UserControl

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary 
                Source="/Your.Assembly.Name;component/TextBlockStyles.xaml"/>
            ... other dictionaries here
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)

您可以缩短Source="..."Source="TextBlockStyles.xaml"文件位于同一程序集和项目根目录中,或者Source="Styles\TextBlockStyles.xaml"将资源字典放入文件夹中Styles.

  • 那么,名为“ TextBlockStyles.xaml”的文件是什么样的呢? (2认同)

Ben*_*Ben 5

用例:您有一个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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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

<Button Style="{StaticResource FooButton}">
    Hello World
</Button>
Run Code Online (Sandbox Code Playgroud)


小智 5

解决方案资源管理器中,右键单击您的项目,选择“添加”,然后单击“资源字典...” ,选择名称并添加到您的项目中。打开App.xaml在应用程序标签 中添加此代码

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="YourStyle.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)

在 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>
Run Code Online (Sandbox Code Playgroud)