C# WPF 资源字典

1、在Window.Resources中声明

注意:这种方式声明的资源只能在当前Window中使用。


1)在Xaml中使用:

 

<Window x:Class="WpfApp_Test.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"
        xmlns:local="clr-namespace:WpfApp_Test"
        mc:Ignorable="d"
        Title="MainWindow" Height="240" Width="300">
    <Window.Resources>
        <ResourceDictionary>
            <Style TargetType="TextBlock" >
                <Setter Property="FontStyle" Value="Italic"/>
                <Setter Property="FontWeight" Value="Bold"/>
                <Setter Property="Foreground" Value="Red"/>
            </Style>
            <Style x:Key="textBlockStyle" TargetType="TextBlock" >
                <Setter Property="Foreground" Value="Green"/>
            </Style>
        </ResourceDictionary>
    </Window.Resources>
    <StackPanel>
        <TextBlock  Text="Hello"/>
        <TextBlock  Text="Hello" Style="{StaticResource textBlockStyle}"/>
        <TextBlock  Text="Hello" Style="{x:Null}"/>
    </StackPanel>
</Window>

 

 

 

运行效果:

 

 

2)在后台代码中使用:

<Window x:Class="WpfApp_Test.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"
        xmlns:local="clr-namespace:WpfApp_Test"
        mc:Ignorable="d"
        Title="MainWindow" Height="240" Width="300">
    <Window.Resources>
        <ResourceDictionary>
            <Style TargetType="TextBlock" x:Key="TextBlockStyle">
                <Setter Property="FontStyle" Value="Italic"/>
                <Setter Property="FontWeight" Value="Bold"/>
                <Setter Property="Foreground" Value="Red"/>
            </Style>
        </ResourceDictionary>
    </Window.Resources>
    <StackPanel>
        <TextBlock x:Name="textBlock1" Text="Hello"/>
        <TextBlock x:Name="textBlock2" Text="Hello"/>
        <TextBlock x:Name="textBlock3" Text="Hello"/>
    </StackPanel>
</Window>


public partial class MainWindow 
{         
    public MainWindow()
    {
        InitializeComponent();
        Style style = (Style)this.FindResource("TextBlockStyle");  //搜索
     //如果明确知道资源放在哪里,则可以使用
     //Style style = (Style)this.Resources["TextBlockStyle"]; textBlock1.Style
= style; } }

运行效果:

 2、在App.xaml中声明(全局可用)

<Application
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp_Test"
             xmlns:av="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="av" x:Class="WpfApp_Test.App"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <Style TargetType="TextBlock" >
                <Setter Property="FontStyle" Value="Italic"/>
                <Setter Property="FontWeight" Value="Bold"/>
                <Setter Property="Foreground" Value="Red"/>
            </Style>
            <Style x:Key="textBlockStyle" TargetType="TextBlock" >
                <Setter Property="Foreground" Value="Green"/>
            </Style>
        </ResourceDictionary>
 
    </Application.Resources>
</Application>


<Window x:Class="WpfApp_Test.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"
        xmlns:local="clr-namespace:WpfApp_Test"
        mc:Ignorable="d"
        Title="MainWindow" Height="240" Width="300">    
    <StackPanel>
        <TextBlock  Text="Hello"/>
        <TextBlock  Text="Hello" Style="{StaticResource textBlockStyle}"/>
        <TextBlock  Text="Hello" />
    </StackPanel>
</Window>

运行效果:

 3、在资源字典文件中声明:

添加资源字典文件的方式,先在项目中新建一个文件夹Resources(也可以不用建文件夹),然后点击右键-——Add——Resource Dictionary(WPF)。 然后把资源添加进去。

 

//TextBlockStyle.xaml:资源字典文件
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="TextBlock" >
        <Setter Property="FontStyle" Value="Italic"/>
        <Setter Property="FontWeight" Value="Bold"/>
        <Setter Property="Foreground" Value="Red"/>
    </Style>
    <Style x:Key="textBlockStyle" TargetType="TextBlock" >
        <Setter Property="Foreground" Value="Green"/>
    </Style>
</ResourceDictionary>


//App.xaml
<Application
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp_Test"
             xmlns:av="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="av" x:Class="WpfApp_Test.App"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/TextBlockStyle.xaml"/>  
       //或者<ResourceDictionary Source="/WpfApp_Test;component/Resources/TextBlockStyle.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application> //MainWindow.xaml <Window x:Class="WpfApp_Test.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" xmlns:local="clr-namespace:WpfApp_Test" mc:Ignorable="d" Title="MainWindow" Height="240" Width="300"> <StackPanel> <TextBlock Text="Hello"/> <TextBlock Text="Hello" Style="{StaticResource textBlockStyle}"/> <TextBlock Text="Hello" /> </StackPanel> </Window>

 最简单的就是直接写文件路径

<ResourceDictionary Source="Resources/TextBlockStyle.xaml"/>

 

注:Style 可以通过BaseOn继承其他Style

//资源字典文件
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="TextBlock" >
        <Setter Property="FontStyle" Value="Italic"/>
        <Setter Property="FontWeight" Value="Bold"/>        
    </Style>
    <Style x:Key="textBlockStyleGreen" TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}">
        <Setter Property="Foreground" Value="Green"/>
    </Style>
    <Style x:Key="textBlockStyleRed" TargetType="TextBlock" >
        <Setter Property="Foreground" Value="Red"/>
    </Style>
</ResourceDictionary>


<Window x:Class="WpfApp_Test.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"
        xmlns:local="clr-namespace:WpfApp_Test"
        mc:Ignorable="d"
        Title="MainWindow" Height="240" Width="300">    
    <StackPanel>
        <TextBlock  Text="Hello"/>
        <TextBlock  Text="Hello" Style="{StaticResource textBlockStyleGreen}"/>
        <TextBlock  Text="Hello" Style="{StaticResource textBlockStyleRed}"/>
    </StackPanel>
</Window>

运行效果:

 

如果资源有key的话,也可以通过key来继承:

 

注意:TargetType可以直接写类型名:

 也可以通过x:Type来写

 

posted @ 2025-06-18 21:25  竹楼风雨声  阅读(42)  评论(0)    收藏  举报