浅谈WPF之样式与资源

news2024/9/30 1:38:29

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.coloradmin.cn/o/1413793.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

ARKit 3D 物体检测跟踪

3D 物体检测跟踪 3D物体检测跟踪技术&#xff0c;是指通过计算机图像处理和人工智能技术对摄像机拍摄到的3D物体识别定位并对其姿态进行跟踪的技术。3D物体跟踪技术的基础也是图像识别&#xff0c;但比前述2D 图像检测、识别、跟踪要复杂得多&#xff0c;原因在于现实世界中的物…

【网络协议测试】畸形数据包——圣诞树攻击(DOS攻击)

简介 TCP所有标志位被设置为1的数据包被称为圣诞树数据包&#xff08;XMas Tree packet&#xff09;&#xff0c;之所以叫这个名是因为这些标志位就像圣诞树上灯一样全部被点亮。 标志位介绍 TCP报文格式&#xff1a; 控制标志&#xff08;Control Bits&#xff09;共6个bi…

单调栈第二天(还没写完)

503.下一个更大元素II 力扣题目链接(opens new window) 给定一个循环数组&#xff08;最后一个元素的下一个元素是数组的第一个元素&#xff09;&#xff0c;输出每个元素的下一个更大元素。数字 x 的下一个更大的元素是按数组遍历顺序&#xff0c;这个数字之后的第一个比它更…

可以运行在浏览器的Windows 2000

Windows 2000 可以在浏览器里跑了&#xff0c;缺点就是速度慢。 点击这里在浏览器中运行 Windows 2000​​​​​​- --------------------------------------------------------------------------------------------------------------------------------- --------------…

2024年【浙江省安全员-C证】考试题库及浙江省安全员-C证模拟考试

题库来源&#xff1a;安全生产模拟考试一点通公众号小程序 2024年【浙江省安全员-C证】考试题库及浙江省安全员-C证模拟考试&#xff0c;包含浙江省安全员-C证考试题库答案和解析及浙江省安全员-C证模拟考试练习。安全生产模拟考试一点通结合国家浙江省安全员-C证考试最新大纲…

vue3+naiveUI二次封装的v-model 联动输入框

根据官网说明使用 源码 <template><div class"clw-input pt-3"><n-inputref"input":value"modelValue":type"type":title"title"clearable:disabled"disabled":size"size"placeholder&…

【异常收集】IDEA启动项目遇到的异常汇总,包括插件异常,版本依赖异常,启动异常等以及对应的解决办法

该文章旨在记录开发中遇到的一些异常&#xff0c;以供遇到似错误进行参考修改 一、项目在多个环境下切换&#xff0c;有一次启动后编译失败&#xff0c;报异常 背景&#xff1a;项目在不同环境下有对应的分支&#xff0c;切换分支后运行项目&#xff0c;报错如下 错误:Kotlin:…

前端工程化之:webpack1-6(编译过程)

一、webpack编译过程 webpack 的作用是将源代码编译&#xff08;构建、打包&#xff09;成最终代码。 整个过程大致分为三个步骤&#xff1a; 初始化编译输出 1.初始化 初始化时我们运行的命令 webpack 为核心包&#xff0c; webpack-cli 提供了 webpack 命令&#xff0c;通过…

YouTrack Pending 项目删除

YouTrack 项目在删除的时候可能没有办法马上就删除掉。 我们还会看到类似下面的这种情况。 根据官方的解释说明是&#xff0c;如果项目有很多内容或者有很多的信息&#xff0c;那么在删除的时候会消耗很多的时间&#xff0c;所以 YouTrack 给出了一个 Pending 删除的状态。 哪…

这些SQL你练习过吗?(网友提供的SQL)

行转列SQL练习 题目 把图1转换成图2结果展示 图1 CREATE TABLE TEST_TB_GRADE (ID int(10) NOT NULL AUTO_INCREMENT,USER_NAME varchar(20) DEFAULT NULL,COURSE varchar(20) DEFAULT NULL,SCORE float DEFAULT 0,PRIMARY KEY (ID) )insert into TEST_TB_GRADE(USER_NAME, CO…

[HTML]Web前端开发技术18(HTML5、CSS3、JavaScript )HTML5 基础与CSS3 应用——喵喵画网页

希望你开心&#xff0c;希望你健康&#xff0c;希望你幸福&#xff0c;希望你点赞&#xff01; 最后的最后&#xff0c;关注喵&#xff0c;关注喵&#xff0c;关注喵&#xff0c;佬佬会看到更多有趣的博客哦&#xff01;&#xff01;&#xff01; 喵喵喵&#xff0c;你对我真的…

Python网络爬虫实战——实验5:Python爬虫之selenium动态数据采集实战

【实验内容】 本实验主要介绍和使用selenium库在js动态加载网页中数据采集的作用。 【实验目的】 1、理解动态加载网页的概念 2、学习Selenium库基本使用 3、掌握动态加载数据采集流程 【实验步骤】 步骤1理解动态加载网页 步骤2学习使用Selenium库 步骤3 采集河北政府采购…

统计学-R语言-8.3

文章目录 前言例题例题一例题二例题三例题四例题五例题六例题七 总结 前言 本篇介绍的是有关方差知识的题目介绍。 例题 例题一 &#xff08;数据&#xff1a;exercise7_3.RData&#xff09;为研究上市公司对其股价波动的关注程度&#xff0c;一家研究机构对在主板、中小板和…

PHP伪协议使用姿势

php支持的伪协议 1 file:// — 访问本地文件系统 2 http:// — 访问 HTTP(s) 网址 3 ftp:// — 访问 FTP(s) URLs 4 php:// — 访问各个输入/输出流&#xff08;I/O streams&#xff09; 5 zlib:// — 压缩流 6 data:// — 数据&#xff08;RFC 2397&#xff09; 7 glob:// —…

YARN介绍

1 概念 YARN 是一个资源管理、任务调度的框架&#xff0c;主要包含三大模块&#xff1a;ResourceManager&#xff08;RM&#xff09;、 NodeManager&#xff08;NM&#xff09;、ApplicationMaster&#xff08;AM&#xff09;。其中&#xff0c;ResourceManager 负责所有资 源…

数据结构——链式二叉树(2)

目录 &#x1f341;一、二叉树的销毁 &#x1f341;二、在二叉树中查找某个数&#xff0c;并返回该结点 &#x1f341;三、LeetCode——检查两棵二叉树是否相等 &#x1f315;&#xff08;一&#xff09;、题目链接&#xff1a;100. 相同的树 - 力扣&#xff08;LeetCode&a…

MySQL十部曲之四:MySQL中的数据类型

文章目录 前言概述数字类型数字类型语法数字类型字面量十六进制字面量位字面量布尔字面量 数字类型的属性超出范围和溢出处理 时间和日期类型时间和日期类型语法DATE、DATETIME和TIMESTAMP的异同TIMESTAMP和DATETIME的自动初始化和更新时间和日期字面量 字符串类型字符串类型语…

Android 基础技术——Handler

笔者希望做一个系列&#xff0c;整理 Android 基础技术&#xff0c;本章是关于 Handler 为什么一个线程对应一个Looper&#xff1f; 核心&#xff1a;通过ThreadLocal保证 Looper.prepare的时候&#xff0c;ThreadLocal.get如果不空报异常&#xff1b;否则调用ThreadLocal.set,…

376. 摆动序列 - 力扣(LeetCode)

题目描述 如果连续数字之间的差严格地在正数和负数之间交替&#xff0c;则数字序列称为摆动序列。第一个差&#xff08;如果存在的话&#xff09;可能是正数或负数。少于两个元素的序列也是摆动序列。 例如&#xff0c; [1,7,4,9,2,5] 是一个摆动序列&#xff0c;因为差值 (6,…

项目中从需求分析到研发上线

一、背景 应用系统从设想到需求到研发到上线会经历一些列工程化过程。比如经典的瀑布模型工作流&#xff0c;其实就是一个经过很多经验总结下来的工程方法。本节阐述项目中从需求到研发上线的过程。但是也有些根据不同的行业&#xff0c;不同的公司&#xff0c;不同管理者的风…