WPF资源基础

1. 使用资源集合

<Window x:Class="项目10.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:项目10"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="500">
    <Window.Resources>
        <ImageBrush x:Name="ImageBrush1" x:Key="TileBrush" TileMode="Tile"
                    ViewportUnits="Absolute" Viewport="0 0 32 32" ImageSource="Images/Jellyfish.jpg"></ImageBrush>
    </Window.Resources>
    <Grid>
        <StackPanel x:Name="StackPanel1" Margin="6">
            <StackPanel.Resources >
                <ImageBrush x:Name="ImageBrush2" x:Key="TileBrush2" TileMode="Tile"
                            ViewportUnits="Absolute" Viewport="0 0 32 32" ImageSource="Images/Chrysanthemum.jpg"></ImageBrush>
            </StackPanel.Resources>
            <Button Content="Button1" x:Name="Button1" Margin="4" FontSize="16" Padding="5"></Button>
            <Button Background="{StaticResource TileBrush}" Content="Button1" x:Name="Button2" Margin="4" FontSize="16" Padding="5"></Button>
            <Button Background="{DynamicResource TileBrush2}" Content="Button1" x:Name="Button3" Margin="4" FontSize="16" Padding="5"></Button>
        </StackPanel>
</Grid>
</Window>

2. 使用字典资源

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:项目10">
    <ImageBrush x:Key="ImageBrush1" TileMode="Tile" ViewportUnits="Absolute" Viewport="0 0 32 32"
                Opacity="0.3" ImageSource="Images/Chrysanthemum.jpg"></ImageBrush>
    <ImageBrush x:Key="ImageBrush2" TileMode="Tile" ViewportUnits="Absolute" Viewport="0 0 32 32"
                Opacity="0.5" ImageSource="Images/Jellyfish.jpg"></ImageBrush>
</ResourceDictionary>
<Application x:Class="项目10.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:项目10"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
         <ResourceDictionary>
             <ResourceDictionary.MergedDictionaries>
                 <ResourceDictionary Source="Dictionary1.xaml"/>
             </ResourceDictionary.MergedDictionaries>
         </ResourceDictionary>
    </Application.Resources>
</Application>
<Window x:Class="项目10.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:项目10"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="500">
<StackPanel Margin="6">
        <Button Padding="15" Margin="6" FontSize="14"
                Background="{DynamicResource ImageBrush1}">OK</Button>
        <Button Padding="15" Margin="6" FontSize="14"
                Background="{DynamicResource ImageBrush2}">OK2</Button>
</StackPanel>
</Window>