C# WPF 資源字典
1、在Window.Resources中聲明
注意:這種方式聲明的資源只能在當(dāng)前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來寫


浙公網(wǎng)安備 33010602011771號