Wpf资源字典-ResourceDictionary

本文介绍了WPF中的资源字典(ResourceDictionary)的使用,它允许在整个项目中定义并重用资源。资源字典区分了StaticResource和DynamicResource,并详细讲述了如何在Windows8应用中使用StaticResource。此外,还讲解了FrameworkElement.Resources和Application.Resources的区别,前者为页面资源,后者为全局应用资源。通过创建资源字典,可以在App.xaml中合并并应用到各个XAML页面。文章通过示例展示了如何定义和应用资源字典,以及如何在Button样式中使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Wpf中的资源(Resources)和资源字典(ResourceDictionary)又有些不同,资源:作用于当前Xaml,若要用于其他Xaml的话又要重新定义。资源字典:给整个项目定义的资源,可用于整个项目中的各Xaml

资源字典(ResourceDictionary)在Wpf应用程序中,XAML资源分为StaticResource(静态资源) 和 DynamicResource(动态资源)。

注意:

  • Windows8应用中,XAML资源仅支持StaticResource(静态资源);
  • 资源应用域不同,XAML资源可分为FrameworkElement.Resources和 Application.Resources
  • FrameworkElement.Resources是将资源对象应用于同一个对象数的不同对象上,称之为页面资源,通常被定义在XAML页面根元素上。
  • Application.Resources是贯穿整个应用级别的资源,通常被定义在App.xaml页面

FrameworkElement.Resources(页面资源):前面已经举例,这里就不详述。

Application.Resourcess(贯穿整个应用级别的资源)

首先要定义资源字典:

  1. 选择应用程序【鼠标右键】­à【添加】­à【资源字典】
  2. 弹出添加新项窗口,选择【资源字典(WPF)】­à名称命名­à点击【添加】创建 资源字典
  3. 创建资源字典成功就可以编写字典了

举个栗子,这次还是来祸害我们的Button:

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

                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

                    xmlns:local="clr-namespace:WpfNorth.Tool.Dictionary">

    <!--新增按钮-->

    <Style x:Key="BtnInsertStyle" TargetType="Button">

        <Setter Property="Width" Value="80"/>

        <Setter Property="Height" Value="30"/>

        <Setter Property="Foreground" Value="White"/>

        <Setter Property="BorderThickness" Value="0"/>

        <Setter Property="Background" Value="#43a9c7"/>

        <Setter Property="Template">

            <Setter.Value>

                <ControlTemplate TargetType="Button">

                    <Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="True">

                        <Grid>

                            <Grid.ColumnDefinitions>

                                <ColumnDefinition Width="1*"/>

                                <ColumnDefinition Width="2*"/>

                            </Grid.ColumnDefinitions>

                            <Image Margin="5" Grid.Column="0" Source="" Stretch="Uniform" VerticalAlignment="Center"/>

                            <!--Text:绑定模板-->

                            <TextBlock Grid.Column="1" Text="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" VerticalAlignment="Center" HorizontalAlignment="Left"/>

                        </Grid>

 

                    </Border>

                    <ControlTemplate.Triggers>

                        <Trigger Property="IsMouseOver" Value="True">

                            <Setter TargetName="border" Property="Background" Value="#2f96b4"/>

                        </Trigger>

                        <Trigger Property="IsPressed" Value="True">

                            <Setter TargetName="border" Property="Background" Value="#2a89a4"/>

                        </Trigger>

                    </ControlTemplate.Triggers>

                </ControlTemplate>

            </Setter.Value>

        </Setter>

    </Style>

    <!--修改按钮-->

    <Style x:Key="BtnUpdateStyle" TargetType="Button">

        <Setter Property="Width" Value="80"/>

        <Setter Property="Height" Value="30"/>

        <Setter Property="Foreground" Value="White"/>

        <Setter Property="BorderThickness" Value="0"/>

        <Setter Property="Background" Value="#43a9c7"/>

        <Setter Property="Template">

            <Setter.Value>

                <ControlTemplate TargetType="Button">

                    <Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="True">

                        <Grid>

                            <Grid.ColumnDefinitions>

                                <ColumnDefinition Width="1*"/>

                                <ColumnDefinition Width="2*"/>

                            </Grid.ColumnDefinitions>

                            <Image Margin="5" Grid.Column="0" Source="" Stretch="Uniform" VerticalAlignment="Center"/>

                            <!--Text:绑定模板-->

                            <TextBlock Grid.Column="1" Text="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" VerticalAlignment="Center" HorizontalAlignment="Left"/>

                        </Grid>

 

                    </Border>

                    <ControlTemplate.Triggers>

                        <Trigger Property="IsMouseOver" Value="True">

                            <Setter TargetName="border" Property="Background" Value="#2f96b4"/>

                        </Trigger>

                        <Trigger Property="IsPressed" Value="True">

                            <Setter TargetName="border" Property="Background" Value="#2a89a4"/>

                        </Trigger>

                    </ControlTemplate.Triggers>

                </ControlTemplate>

            </Setter.Value>

        </Setter>

    </Style>

    <!--删除按钮-->

    <Style x:Key="BtnDeleteStyle" TargetType="Button">

        <Setter Property="Width" Value="80"/>

        <Setter Property="Height" Value="30"/>

        <Setter Property="Foreground" Value="White"/>

        <Setter Property="BorderThickness" Value="0"/>

        <Setter Property="Background" Value="#43a9c7"/>

        <Setter Property="Template">

            <Setter.Value>

                <ControlTemplate TargetType="Button">

                    <Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="True">

                        <Grid>

                            <Grid.ColumnDefinitions>

                                <ColumnDefinition Width="1*"/>

                                <ColumnDefinition Width="2*"/>

                            </Grid.ColumnDefinitions>

                            <Image Margin="5" Grid.Column="0" Source="" Stretch="Uniform" VerticalAlignment="Center"/>

                            <!--Text:绑定模板-->

                            <TextBlock Grid.Column="1" Text="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" VerticalAlignment="Center" HorizontalAlignment="Left"/>

                        </Grid>

 

                    </Border>

                    <ControlTemplate.Triggers>

                        <Trigger Property="IsMouseOver" Value="True">

                            <Setter TargetName="border" Property="Background" Value="#2f96b4"/>

                        </Trigger>

                        <Trigger Property="IsPressed" Value="True">

                            <Setter TargetName="border" Property="Background" Value="#2a89a4"/>

                        </Trigger>

                    </ControlTemplate.Triggers>

                </ControlTemplate>

            </Setter.Value>

        </Setter>

    </Style>

    <!--打印按钮-->

    <Style x:Key="BtnPrintStyle" TargetType="Button">

        <Setter Property="Width" Value="80"/>

        <Setter Property="Height" Value="30"/>

        <Setter Property="Foreground" Value="White"/>

        <Setter Property="BorderThickness" Value="0"/>

        <Setter Property="Background" Value="#43a9c7"/>

        <Setter Property="Template">

            <Setter.Value>

                <ControlTemplate TargetType="Button">

                    <Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="True">

                        <Grid>

                            <Grid.ColumnDefinitions>

                                <ColumnDefinition Width="1*"/>

                                <ColumnDefinition Width="2*"/>

                            </Grid.ColumnDefinitions>

                            <Image Margin="5" Grid.Column="0" Source="" Stretch="Uniform" VerticalAlignment="Center"/>

                            <!--Text:绑定模板-->

                            <TextBlock Grid.Column="1" Text="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" VerticalAlignment="Center" HorizontalAlignment="Left"/>

                        </Grid>

 

                    </Border>

                    <ControlTemplate.Triggers>

                        <Trigger Property="IsMouseOver" Value="True">

                            <Setter TargetName="border" Property="Background" Value="#2f96b4"/>

                        </Trigger>

                        <Trigger Property="IsPressed" Value="True">

                            <Setter TargetName="border" Property="Background" Value="#2a89a4"/>

                        </Trigger>

                    </ControlTemplate.Triggers>

                </ControlTemplate>

            </Setter.Value>

        </Setter>

    </Style>

 

    <!--保存按钮-->

    <Style x:Key="BtnSaveStyle" TargetType="Button">

        <Setter Property="Width" Value="80"/>

        <Setter Property="Height" Value="30"/>

        <Setter Property="Foreground" Value="White"/>

        <Setter Property="BorderThickness" Value="0"/>

        <Setter Property="Background" Value="#43a9c7"/>

        <Setter Property="Template">

            <Setter.Value>

                <ControlTemplate TargetType="Button">

                    <Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="True">

                        <Grid>

                            <Grid.ColumnDefinitions>

                                <ColumnDefinition Width="1*"/>

                                <ColumnDefinition Width="2*"/>

                            </Grid.ColumnDefinitions>

                            <Image Margin="5" Grid.Column="0" Source="" Stretch="Uniform" VerticalAlignment="Center"/>

                            <!--Text:绑定模板-->

                            <TextBlock Grid.Column="1" Text="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" VerticalAlignment="Center" HorizontalAlignment="Left"/>

                        </Grid>

 

                    </Border>

                    <ControlTemplate.Triggers>

                        <Trigger Property="IsMouseOver" Value="True">

                            <Setter TargetName="border" Property="Background" Value="#2f96b4"/>

                        </Trigger>

                        <Trigger Property="IsPressed" Value="True">

                            <Setter TargetName="border" Property="Background" Value="#2a89a4"/>

                        </Trigger>

                    </ControlTemplate.Triggers>

                </ControlTemplate>

            </Setter.Value>

        </Setter>

    </Style>

    <!--关闭按钮-->

    <Style x:Key="BtnCloseStyle" TargetType="Button">

        <Setter Property="Width" Value="80"/>

        <Setter Property="Height" Value="30"/>

        <Setter Property="Foreground" Value="White"/>

        <Setter Property="BorderThickness" Value="0"/>

        <Setter Property="Background" Value="#43a9c7"/>

        <Setter Property="Template">

            <Setter.Value>

                <ControlTemplate TargetType="Button">

                    <Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="True">

                        <Grid>

                            <Grid.ColumnDefinitions>

                                <ColumnDefinition Width="1*"/>

                                <ColumnDefinition Width="2*"/>

                            </Grid.ColumnDefinitions>

                            <Image Margin="5" Grid.Column="0" Source="" Stretch="Uniform" VerticalAlignment="Center"/>

                            <!--Text:绑定模板-->

                            <TextBlock Grid.Column="1" Text="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" VerticalAlignment="Center" HorizontalAlignment="Left"/>

                        </Grid>

 

                    </Border>

                    <ControlTemplate.Triggers>

                        <Trigger Property="IsMouseOver" Value="True">

                            <Setter TargetName="border" Property="Background" Value="#2f96b4"/>

                        </Trigger>

                        <Trigger Property="IsPressed" Value="True">

                            <Setter TargetName="border" Property="Background" Value="#2a89a4"/>

                        </Trigger>

                    </ControlTemplate.Triggers>

                </ControlTemplate>

            </Setter.Value>

        </Setter>

    </Style>

</ResourceDictionary>

写好资源字典后,下一步就是在App.xaml中合并资源字典:

<Application x:Class="WpfNorth.App"

             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

             xmlns:local="clr-namespace:WpfNorth"

             StartupUri="LoginWindow.xaml">

    <Application.Resources>

        <ResourceDictionary>

            <ResourceDictionary.MergedDictionaries>

                <!--封装好的公共样式-->

                <ResourceDictionary Source="Tool/Dictionary/Buttons.xaml"/>

            </ResourceDictionary.MergedDictionaries>

        </ResourceDictionary>

    </Application.Resources>

</Application>

再然后就是应用了,创建一个button按钮然后应用资源:

<Button x:Name=”button” Content=” 删除按钮” Style=”{StaticResource BtnDeleteStyle}”/>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值