浅谈WPF之样式与资源

WPF通过样式,不仅可以方便的设置控件元素的展示方式,给用户呈现多样化的体验,还简化配置,避免重复设置元素的属性,以达到节约成本,提高工作效率的目的,样式也是资源的一种表现形式。本文以一个简单的小例子,简述如何设置WPF的样式以及资源的应用,仅供学习分享使用,如有不足之处,还请指正。

图片

什么是样式?

样式(Style)是组织和重用格式化选项的重要工具。不是使用重复的标记填充XAML,以便设置外边距、内边距、颜色以及字体等细节,而是创建一系列封装所有这些细节的样式,然后再需要之处通过属性来应用样式。

样式是可应用于元素的属性值集合。使用资源的最常见原因之一就是样式。

基础样式

1. 通过TargetType设置样式

通过控件类型,统一设置样式【如:字体,大小,边距等】,以便于形成统一的风格。如下所示:

图片

通过设置样式的TargetType="Button",则可以使所有的按钮应用同一个样式,统一风格。如下所示:

<Window x:Class="WpfApp1.SevenWindow"
        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:WpfApp1"
        mc:Ignorable="d"
        Title="基础样式示例" Height="250" Width="400">
    <Window.Resources>
        <Style  TargetType="Button"  >
            <Setter Property="Button.Margin" Value="2,5,2,5"></Setter>
            <Setter Property="Control.FontFamily" Value="SimSun-ExtB"></Setter>
            <Setter Property="Control.FontSize" Value="18"></Setter>
</Style>
    </Window.Resources>
    <StackPanel>
        <Button x:Name="button1" Content="第一个按钮"></Button>
        <Button x:Name="button2" Content="第二个按钮" ></Button>
        <Button x:Name="button3" Content="第三个按钮"></Button>
        <Button x:Name="button4" Content="第四个按钮" ></Button>
    </StackPanel>
</Window>

2. 通过Key设置样式

如果需要对每一个控件元素,都设置不同的样式,则可以通过不同的Key加以区分,如下所示:

图片

分别设置不同的样式,每一个样式都有一个唯一的Key,然后分别绑定到各个元素的Style属性上,如下所示:

<Window x:Class="WpfApp1.SevenWindow"
        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:WpfApp1"
        mc:Ignorable="d"
        Title="基础样式示例" Height="250" Width="400">
    <Window.Resources>
        <Style TargetType="Button" >
            <Setter Property="Button.Margin" Value="2,5,2,5"></Setter>
            <Setter Property="Control.FontFamily" Value="SimSun-ExtB"></Setter>
            <Setter Property="Control.FontSize" Value="16"></Setter>
        </Style>
        <Style x:Key="first">
            <Setter Property="Control.Foreground" Value="Red"></Setter>
            <Setter Property="Control.Background" Value="Gray"></Setter>
        </Style>
        <Style x:Key="second">
            <Setter Property="ItemsControl.Foreground" Value="Gold"></Setter>
            <Setter Property="ItemsControl.Background" Value="DarkCyan"></Setter>
        </Style>
        <Style x:Key="third">
            <Setter Property="ItemsControl.Foreground" Value="White"></Setter>
            <Setter Property="ItemsControl.Background" Value="DarkRed"></Setter>
        </Style>
        <Style x:Key="four">
            <Setter Property="ItemsControl.Foreground" Value="Blue"></Setter>
            <Setter Property="ItemsControl.Background" Value="LightCoral"></Setter>
        </Style>
    </Window.Resources>
    <StackPanel>
        <Button x:Name="button1" Content="第一个按钮" Style="{StaticResource first}"></Button>
        <Button x:Name="button2" Content="第二个按钮" Style="{StaticResource second}"></Button>
        <Button x:Name="button3" Content="第三个按钮" Style="{StaticResource third}"></Button>
        <Button x:Name="button4" Content="第四个按钮" Style="{StaticResource four}"></Button>
    </StackPanel>
</Window>

3. 样式继承

通过仔细观察发现,在设置了单独样式以后,统一的样式失去了作用,说明每一个元素控件,只能绑定一个样式,那怎么办才能让统一样式起作用呢?答案就是面向对象思想中的继承。

在WPF中,通过设置BasedOn来继承父样式,如下所示:

图片

在每一个样式通过BasedOn属性继承父样式,如下所示:

<Window x:Class="WpfApp1.SevenWindow"
        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:WpfApp1"
        mc:Ignorable="d"
        Title="基础样式示例" Height="250" Width="400">
    <Window.Resources>
        <Style x:Key="base" >
            <Setter Property="Control.Margin" Value="2,5,2,5"></Setter>
            <Setter Property="Control.FontFamily" Value="SimSun-ExtB"></Setter>
            <Setter Property="Control.FontSize" Value="18"></Setter>
        </Style>
        <Style x:Key="first" BasedOn="{StaticResource base}">
            <Setter Property="Control.Foreground" Value="Red"></Setter>
            <Setter Property="Control.Background" Value="Gray"></Setter>
        </Style>
        <Style x:Key="second" BasedOn="{StaticResource base}">
            <Setter Property="ItemsControl.Foreground" Value="Gold"></Setter>
            <Setter Property="ItemsControl.Background" Value="DarkCyan"></Setter>
        </Style>
        <Style x:Key="third" BasedOn="{StaticResource base}">
            <Setter Property="ItemsControl.Foreground" Value="White"></Setter>
            <Setter Property="ItemsControl.Background" Value="DarkRed"></Setter>
        </Style>
        <Style x:Key="four" BasedOn="{StaticResource base}">
            <Setter Property="ItemsControl.Foreground" Value="Blue"></Setter>
            <Setter Property="ItemsControl.Background" Value="LightCoral"></Setter>
        </Style>
    </Window.Resources>
    <StackPanel>
        <Button x:Name="button1" Content="第一个按钮" Style="{StaticResource first}"></Button>
        <Button x:Name="button2" Content="第二个按钮" Style="{StaticResource second}"></Button>
        <Button x:Name="button3" Content="第三个按钮" Style="{StaticResource third}"></Button>
        <Button x:Name="button4" Content="第四个按钮" Style="{StaticResource four}"></Button>
    </StackPanel>
</Window>

注意:如果样式要被其他样式继承,则最好不要使用TargetType指定。一般情况下,可能为报错【只能根据带有基类型“IFrameworkInputElement”的目标类型的 Style。】

4. 样式中绑定事件

在WPF中的样式中,通过EventSetter进行事件绑定,如下所示:

图片

在样式中,通过EventSetter设置事件,如下所示:

<Window x:Class="WpfApp1.SevenWindow"
        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:WpfApp1"
        mc:Ignorable="d"
        Title="基础样式示例" Height="250" Width="400">
    <Window.Resources>
        <Style x:Key="base">
            <Setter Property="Control.Margin" Value="2,5,2,5"></Setter>
            <Setter Property="Control.FontFamily" Value="SimSun-ExtB"></Setter>
            <Setter Property="Control.FontSize" Value="18"></Setter>
        </Style>
        <Style x:Key="first" BasedOn="{StaticResource base}">
            <Setter Property="Control.Foreground" Value="Red"></Setter>
            <Setter Property="Control.Background" Value="Gray"></Setter>
            <EventSetter Event="Button.MouseEnter" Handler="FirstButton_MouseEnter"></EventSetter>
        </Style>
    </Window.Resources>
    <StackPanel>
        <Button x:Name="button1" Content="第一个按钮" Style="{StaticResource first}"></Button>
    </StackPanel>
</Window>

其中FirstButton_MouseEnter,文后台定义的一个事件函数,如下所示:​​​​​​​

private void FirstButton_MouseEnter(object sender,MouseEventArgs e)
{
      Button btn = (Button)sender;
      MessageBox.Show("鼠标进入了 "+btn.Content.ToString()+" 呀!");
}
 

触发器

使用触发器可自动完成简单的样式的改变,不需要使用代码,也可以完成不少工作触发器通过Style.Trigger集合链接到样式。每个样式可以有任意多个触发器。每个触发器都是System.Windows.TriggerBase的实例。

TriggerBase的子类

图片

1. 基础触发器

触发器,是指当满足一定条件,然后触发相关的样式设置,如下所示:

图片

示例中设置了两个触发器:1.Control.IsMouseOver鼠标覆盖在按钮上时,设置对应的样式。2. Control.IsFocused,控件聚焦时,设置对应的样式。如下所示:

<Window x:Class="WpfApp1.EightWindow"
        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:WpfApp1"
        mc:Ignorable="d"
        Title="EightWindow" Height="350" Width="400">
    <Window.Resources>
        <Style x:Key="first">
            <Setter Property="Control.Margin" Value="2,5,2,5"></Setter>
            <Setter Property="Control.FontFamily" Value="SimSun-ExtB"></Setter>
            <Setter Property="Control.FontSize" Value="18"></Setter>
            <Setter Property="Control.Foreground" Value="Red"></Setter>
            <Setter Property="Control.Background" Value="LightBlue"></Setter>
            <Style.Triggers>
                <Trigger Property="Control.IsMouseOver" Value="True">
                    <Setter Property="ItemsControl.Background" Value="AliceBlue"></Setter>
                    <Setter Property="Control.FontSize" Value="28"></Setter>
                </Trigger>
                <Trigger Property="Control.IsFocused" Value="True">
                    <Setter Property="ItemsControl.Background" Value="DarkGoldenrod"></Setter>
                    <Setter Property="Control.FontSize" Value="28"></Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <StackPanel>
        <Button x:Name="button1" Content="第一个按钮" Style="{StaticResource first}"></Button>
    </StackPanel>
</Window>

注意:如果样式触发器,设置了多个,且条件相互覆盖时,以最后的设置为准

2. 多条件触发器

如果需要多个条件同时满足,才能设置对应的样式,则可以通过MultiTrigger来设置,如下所示:

<Window x:Class="WpfApp1.EightWindow"
        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:WpfApp1"
        mc:Ignorable="d"
        Title="EightWindow" Height="350" Width="400">
    <Window.Resources>
        <Style x:Key="first">
            <Setter Property="Control.Margin" Value="2,5,2,5"></Setter>
            <Setter Property="Control.FontFamily" Value="SimSun-ExtB"></Setter>
            <Setter Property="Control.FontSize" Value="18"></Setter>
            <Setter Property="Control.Foreground" Value="Red"></Setter>
            <Setter Property="Control.Background" Value="LightBlue"></Setter>
            <Style.Triggers>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="Control.IsMouseOver" Value="True"></Condition>
                        <Condition Property="Control.IsFocused" Value="True"></Condition>
                    </MultiTrigger.Conditions>
                    <MultiTrigger.Setters>
                        <Setter Property="ItemsControl.Background" Value="Gainsboro"></Setter>
                        <Setter Property="Control.FontSize" Value="20"></Setter>
                    </MultiTrigger.Setters>
                </MultiTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <StackPanel>
        <Button x:Name="button1" Content="第一个按钮" Style="{StaticResource first}"></Button>
        <Button x:Name="button2" Content="第二个按钮" ></Button>
    </StackPanel>
</Window>

3. 事件触发器

事件触发器,是指某一个事件发生时,触发的相关动作,主要用于动画,如下所示:

图片

当鼠标进入时,字体变大,当鼠标离开时,字体恢复,如下所示:​​​​​​​

<Window x:Class="WpfApp1.EightWindow"
        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:WpfApp1"
        mc:Ignorable="d"
        Title="EightWindow" Height="350" Width="400">
    <Window.Resources>
        <Style x:Key="first">
           <Setter Property="Control.Margin" Value="2,5,2,5"></Setter>
           <Setter Property="Control.FontFamily" Value="SimSun-ExtB"></Setter>
           <Setter Property="Control.FontSize" Value="18"></Setter>
           <Setter Property="Control.Foreground" Value="Red"></Setter>
           <Setter Property="Control.Background" Value="LightBlue"></Setter>
           <Style.Triggers>
               <EventTrigger RoutedEvent="Mouse.MouseEnter" >
                   <EventTrigger.Actions>
                       <BeginStoryboard>
                           <Storyboard>
                               <DoubleAnimation Duration="00:00:02" To="28" From="12" Storyboard.TargetProperty="FontSize"></DoubleAnimation>
                            </Storyboard>
                        </BeginStoryboard>
                   </EventTrigger.Actions>
                </EventTrigger>
                <EventTrigger RoutedEvent="Mouse.MouseLeave">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Duration="00:00:01" Storyboard.TargetProperty="FontSize" To="18"  />
                            </Storyboard>
                        </BeginStoryboard>
                   </EventTrigger.Actions>
                </EventTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <StackPanel>
        <Button x:Name="button1" Content="第一个按钮" Style="{StaticResource first}"></Button>
        <Button x:Name="button2" Content="第二个按钮" ></Button>
    </StackPanel>
</Window>

什么是资源?

资源是可以在应用程序中的不同位置重复使用的对象。WPF不仅支持传统的程序级的资源,还有独具特色的对象级资源,每一个界面元素,都可以拥有自己的资源,并被子元素共享。

资源基础用法

通常情况下,资源是在Window.Resources节点下,便于Window下所有的子元素共享,如下示例所示:

图片

定义一个字符串类型的资源,在TextBlock中通过Text="{StaticResource default}"的方式进行引用。如下所示:

<Window x:Class="WpfApp1.TenWindow"
        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:WpfApp1"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="资源基础示例" Height="250" Width="400">
    <Window.Resources>
        <sys:String x:Key="default">
            沉舟侧畔千帆过,病树前头万木春
        </sys:String>
    </Window.Resources>
    <Grid>
        <TextBlock x:Name="tbInfo" Text="{StaticResource default}" FontSize="20" Margin="10" Padding="10" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
    </Grid>
</Window>

资源层级

WPF资源是采用从内到外,逐层进行查找的,如果在当前窗口未检索到资源,则继续到App.xaml中继续查找,示例如下所示:

图片

 在App.xaml中定义资源,然后在Window中应用资源,如下所示:

<Application x:Class="WpfApp1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp1"
             xmlns:sys="clr-namespace:System;assembly=mscorlib"
             StartupUri="TenWindow.xaml">
    <Application.Resources>
        <sys:String x:Key="story">
            怀旧空吟闻笛赋,到乡翻似烂柯人。
        </sys:String>
    </Application.Resources>
</Application>

在Window窗口中调用,和调用本地资源是一样的,如下所示:​​​​​​​

<Window x:Class="WpfApp1.TenWindow"
        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:WpfApp1"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="资源基础示例" Height="250" Width="400">
    <Window.Resources>
        <sys:String x:Key="default">
            沉舟侧畔千帆过,病树前头万木春。
        </sys:String>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <TextBlock x:Name="tbInfo1" Grid.Row="0" Text="{StaticResource story}" FontSize="20" Margin="10" Padding="10" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
        <TextBlock x:Name="tbInfo2" Grid.Row="1" Text="{StaticResource default}" FontSize="20" Margin="10" Padding="10" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
    </Grid>
</Window>

资源分类

根据资源的加载时间点,资源分为两类,如下所示:

  1. 静态资源:静态资源是在程序启动初始化时进行加载且只加载一次的资源

  2. 动态资源:动态资源是在程序执行过程中,动态的去访问资源,会随着资源的改变而改变,所以动态资源对系统的消耗相对比较大

动态资源

上述的基础示例,采用的是静态资源的方式。动态资源则是在程序执行过程中随着资源的改变而改变。

两个按钮使用同一个资源【背景图片】,只是一个采用静态资源引用,一个采用动态资源引用,当资源发生改变时,一个不改变,一个实时变化。如下所示:

图片

 示例源码,如下所示:

<Window x:Class="WpfApp1.NineWindow"
        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:WpfApp1"
        mc:Ignorable="d"
        Title="资源基础示例" Height="320" Width="400">
    <Window.Resources>
        <!--ViewportUnits——设置平铺的相对/绝对坐标,即图片在哪平铺。-->
        <ImageBrush x:Key="one" Viewport="0 0 50 50" ViewportUnits="Absolute" TileMode="Tile" ImageSource="alan_logo.png" Opacity="0.3"></ImageBrush>
    </Window.Resources>
    <StackPanel Margin="5" x:Name="stackpanel1">
        <Button Content="第一个按钮" Name="first" Margin="5" Padding="25" FontSize="58" Background="{ StaticResource one}"></Button>
        <Button Content="第二个按钮" Name="second" Margin="5" Padding="25" FontSize="58" Background="{ DynamicResource one}" Click="second_Click" ></Button>
    </StackPanel>
</Window>

后台修改资源的代码如下所示:

private void second_Click(object sender, RoutedEventArgs e)
{
       var img = this.FindResource("one") as ImageBrush ;
       img = new ImageBrush(new BitmapImage(new Uri(@"imgs/alan_logo1.png", UriKind.Relative)));
       img.TileMode = TileMode.Tile;
       img.Opacity = 0.3;
       img.Viewport = new Rect(0, 0, 50, 50);
       img.ViewportUnits = BrushMappingMode.Absolute;
       this.Resources["one"] = img;
       //注意:此处是直接重写覆盖资源key=one的对象,并不是对原资源设置ImageSoure属性。两者效果不同
}

资源文件

资源文件位于Properties/Resources.resx中,如果想要在程序中访问资源文件的内容,则必须将访问修饰符设置成public,如下所示:

图片

在WPF中,通过Text="{x:Static prop:Resources.Password}"的方式,进行访问资源内容,示例如下:

图片

 示例源码如下:​​​​​​​

<Window x:Class="WpfApp1.ElevenWindow"
        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:WpfApp1"
        xmlns:prop="clr-namespace:WpfApp1.Properties"
        mc:Ignorable="d"
        Title="资源文件示例" Height="150" Width="400">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"></ColumnDefinition>
            <ColumnDefinition Width="2*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <TextBlock x:Name="tbUserName" Text="{x:Static prop:Resources.UserName}" VerticalAlignment="Center" HorizontalAlignment="Right"  Grid.Row="0" Grid.Column="0" Margin="5"></TextBlock>
        <TextBox x:Name="txtUserName" Grid.Row="0" Grid.Column="1" Margin="5"></TextBox>
        <TextBlock x:Name="tbPassword" Text="{x:Static prop:Resources.Password}"  VerticalAlignment="Center" HorizontalAlignment="Right"   Grid.Row="1" Grid.Column="0" Margin="5"></TextBlock>
        <TextBox x:Name="txtPassword" Grid.Row="1" Grid.Column="1" Margin="5"></TextBox>
        <Button x:Name="btnSubmit" Content="{x:Static prop:Resources.Submit}" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" Width="150" Margin="5"></Button>
    </Grid>
</Window>

资源字典

资源字典可以实现资源的共享,一份定义,多处使用的效果。具有可维护性,高效,适应性等优势

首先创建资源字典文件,通过程序右键--添加--资源字典,打开资源字典对话框,创建名称为OneDictionary.xaml,如下所示:

图片

 资源字典中创建了五个资源,如下所示:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:sys="clr-namespace:System;assembly=mscorlib"
                    xmlns:local="clr-namespace:WpfApp1">
    <sys:String x:Key="story0">酬乐天扬州初逢席上见赠</sys:String>
    <sys:String x:Key="story1">【作者】刘禹锡 【朝代】唐</sys:String>
    <sys:String x:Key="story2">巴山楚水凄凉地,二十三年弃置身。</sys:String>
    <sys:String x:Key="story3">怀旧空吟闻笛赋,到乡翻似烂柯人。</sys:String>
    <sys:String x:Key="story4">沉舟侧畔千帆过,病树前头万木春。</sys:String>
    <sys:String x:Key="story5">今日听君歌一曲,暂凭杯酒长精神。</sys:String>
</ResourceDictionary>

在对应窗口中,包含资源文件的路径即可,如下所示:

<Window x:Class="WpfApp1.TwelveWindow"
        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:WpfApp1"
        mc:Ignorable="d"
        Title="资源字典示例" Height="350" Width="400">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="OneDictionary.xaml"></ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <StackPanel Margin="5" HorizontalAlignment="Center">
        <TextBlock x:Name="tbStory0" Margin="5" Padding="5" FontSize="20"  Text="{StaticResource story0}"></TextBlock>
        <TextBlock x:Name="tbStory1" Margin="5" Padding="5" FontSize="20"  Text="{StaticResource story1}"></TextBlock>
        <TextBlock x:Name="tbStory2" Margin="5" Padding="5" FontSize="20"  Text="{StaticResource story2}"></TextBlock>
        <TextBlock x:Name="tbStory3" Margin="5" Padding="5" FontSize="20"  Text="{StaticResource story3}"></TextBlock>
        <TextBlock x:Name="tbStory4" Margin="5" Padding="5" FontSize="20"  Text="{StaticResource story4}"></TextBlock>
        <TextBlock x:Name="tbStory5" Margin="5" Padding="5" FontSize="20"  Text="{StaticResource story5}"></TextBlock>
    </StackPanel>
</Window>

示例截图如下:

图片

以上就是【浅谈WPF之样式与资源】的全部内容,关于更多详细内容,可参考官方文档。希望能够一起学习,共同进步。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/351903.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

数学建模论文笔记

一、概述 1. 数学建模论文组成 论文电子版&#xff1a;摘要页、正文、参考文献、附录支撑材料&#xff1a;源程序代码以及调用说明、中间结果、支撑数据等首页&#xff1a;论文题目、摘要、关键词论文正文&#xff1a;问题重述、问题分析、模型假设、符号说明、模型建立与求解…

centos 7 增加临时路由及永久路由

centos 7 增加临时路由及永久路由 如果增加临时路由&#xff0c;要先安装net-tools , sudo yum install net-tools route add -net 10.1.0.0 gw 10.1.1.1 netmask 255.255.0.0 意思是增加了一条动态路由&#xff0c;网关10.1.1.1 ,10.1.x.x 的所有ip都走这个网关 此种方式&am…

常见OLAP对比

Olap&#xff08;On-line Analytical Processing&#xff0c;联机分析处理&#xff09;&#xff1a;是在基于数据仓库多维模型的基础上实现的面向分析的各类操作的集合。可以比较下其与传统的OLTP&#xff08;On-line Transaction Processing&#xff0c;联机事务处理&#xff…

C语言第十弹---函数(上)

✨个人主页&#xff1a; 熬夜学编程的小林 &#x1f497;系列专栏&#xff1a; 【C语言详解】 【数据结构详解】 函数 1、函数的概念 2、库函数 2.1、标准库和头文件 2.2、库函数的使用方法 2.2.1、功能 2.2.2、头文件包含 2.2.3、实践 2.2.4、库函数文档的⼀般格式 …

Unity中URP下额外灯的距离衰减

文章目录 前言一、额外灯的距离衰减二、DistanceAttenuation函数的传入参数1、distanceSqr2、distanceAndSpotAttenuation3、_AdditionalLightsAttenuation4、GetPunctualLightDistanceAttenuation函数三、DistanceAttenuation函数的程序体 前言 在上一篇文章中&#xff0c;我…

组件冲突、data函数、组件通信

文章目录 1.组件的三大组成部分 - 注意点说明2.组件的样式冲突&#xff08;用 scoped 解决&#xff09;3.data是一个函数4.组件通信1.什么是组件通信&#xff1f;2.不同的组件关系 和 组件通信方案分类 5.prop详解prop 校验①类型校验②完整写法&#xff08;类型&#xff0c;非…

计算机毕业设计 | SSM 凌云招聘平台(附源码)

1&#xff0c;绪论 人力资源是企业产生效益、创造利润的必不可少的、最重要的资源。人作为人力资源的个体可看作是一个承载着有效知识、能力的信息单元。这样的信息单元可看作是一个为企业产生价值和利润的个体。从而使得这样的信息单元所具有的信息就是一个有价值的信息。 校…

什么是SQL,什么是MYSQL?MYSQL的架构以及SQL执行语句的过程是什么?有哪些数据库的类型?一篇文章带你弄懂!

文章目录 前言一、为什么需要数据库二、数据库的相关概念1.什么是结构化查询语言 (SQL)2.什么是数据库管理系统 (DBMS)3.什么是 MySQL 数据库 三、数据库分类1.关系型数据库&#xff08;SQL&#xff09;2.非关系型数据库&#xff08;NoSQL&#xff09; 四、MYSQL架构1.各组件功…

初识MQRabbitMQ快速入门

一、同步和异步通讯 微服务间通讯有同步和异步两种方式&#xff1a; 同步通讯&#xff1a;就像打电话&#xff0c;需要实时响应。 异步通讯&#xff1a;就像发邮件&#xff0c;不需要马上回复。 两种方式各有优劣&#xff0c;打电话可以立即得到响应&#xff0c;但是你却不能…

「JavaSE」类和对象4

类和对象4 &#x1f349;内部类&#x1f34c;实例内部类&#x1f34c;静态内部类&#x1f34c;局部内部类&#x1f34c;匿名内部类 &#x1f349;总结 &#x1f349;内部类 在 Java 中&#xff0c;我们可以将一个类定义在另一个类或者一个方法的内部&#xff0c;前者称为内部类…

IS-IS:04 DIS

IS-IS 协议只支持两种网络类型&#xff0c;即广播网络和点到点网络。与 OSPF 协议相同&#xff0c; IS-IS 协议在广播网络中会将网络视为一个伪节点 &#xff08; Pesudonde&#xff0c;简称 PSN&#xff09;&#xff0c;并选举出一台DIS &#xff08;Designated IS&#xff09…

探索Pyecharts之美-绘制多彩旭日图的艺术与技巧【第37篇—python:旭日图】

文章目录 引言准备工作绘制基本旭日图调整颜色和样式添加交互功能定制标签和标签格式嵌套层级数据高级样式与自定义进阶主题&#xff1a;动态旭日图数据源扩展&#xff1a;外部JSON文件总结 引言 数据可视化在现代编程中扮演着重要的角色&#xff0c;而Pyecharts是Python中一个…

Tomcat怎么优化

目录 性能方面的优化&#xff1a; 安全方面的优化&#xff1a; 引言&#xff1a;面试官问到的Tomcat怎么优化&#xff0c;这两个方面直接得到他认可&#xff01;&#xff01; 性能方面的优化&#xff1a; 内存优化&#xff1a;-Xms java虚拟机初始化时的最小内存、-Xmx java虚…

操作系统的引入

操作系统 【1】什么是操作系统 操作系统是一种管理的计算机硬件的软件资源的程序。它充当了计算机系统和应用程序之间的接口。使得计算机用户能够地使用计算机系统来完成各种任务。操作系统是负责管理和分配计算机的处理器、内存、硬盘等等硬件资源&#xff0c;同时也提供一些…

Vue3在css中使用v-bind绑定js/ts变量,也可以在scss和less中使用方式

主要介绍Vue3中的新增的v-bind()的常用使用方式&#xff0c;主要包括在css,less,scss中的使用&#xff0c;可以参考官方文档查看&#xff1a;Vue3官方文档 特别提醒 如果你想在scss中或者less中使用&#xff0c;可能会报各种乱七八糟的错误&#xff0c;最快最好用的方式就是单…

Android P 背光机制流程分析

在android 9.0中&#xff0c;相比android 8.1而言&#xff0c;背光部分逻辑有较大的调整&#xff0c;这里就对android P背光机制进行完整的分析。 1.手动调节亮度 1.1.在SystemUI、Settings中手动调节 在界面(SystemUI)和Settings中拖动进度条调节亮度时&#xff0c;调节入口…

[docker] Docker的私有仓库部署——Harbor

一、Docker原生私有仓库—— Registry 1.1 Registry的简单了解 关于Docker的仓库分为私有库和公有仓库&#xff0c;共有仓库只要在官方注册用户&#xff0c;登录即可使用。但对于仓库的使用&#xff0c;企业还是会有自己的专属镜像&#xff0c;所以私有库的搭建也是很有必要的…

那些年与指针的爱恨情仇(一)---- 指针本质及其相关性质用法

关注小庄 顿顿解馋 (≧∇≦) 引言&#xff1a; 小伙伴们在学习c语言过程中是否因为指针而困扰&#xff0c;指针简直就像是小说女主&#xff0c;它逃咱追&#xff0c;我们插翅难飞…本篇文章让博主为你打理打理指针这个傲娇鬼吧~ 本节我们将认识到指针本质&#xff0c;何为指针和…

将Html页面转换为Wordpress页面

问题&#xff1a;我们经常会从html源码下载网站上获得我们想要的网站内容框架&#xff0c;以及部分诸如联系我们&#xff0c;About 等内页&#xff0c;但是在文章的发布上&#xff0c;则远不如Wordpress简便。而Wordpress尽管有各种模板&#xff0c;但修改又比较麻烦。解决方法…

day34WEB 攻防-通用漏洞文件上传黑白盒审计逻辑中间件外部引用

目录 一&#xff0c;白盒审计-Finecms-代码常规-处理逻辑 黑盒思路&#xff1a;寻找上传点抓包修改突破获取状态码及地址 审计流程&#xff1a;功能点-代码文件-代码块-抓包调试-验证测试 二&#xff0c;白盒审计-CuppaCms-中间件-.htaccess 三&#xff0c;白盒审计-Metin…
最新文章