写点什么

WPF - 资源引用、资源字典引用以及容器模板和数据模板

作者:@@神农
  • 2022 年 3 月 15 日
  • 本文字数:4847 字

    阅读完需:约 16 分钟

一、资源引用

​ 语法:


//基本语法pack://application:,,,/项目名称;component/文件名称  //引用字体<TextBlock Text="&#xe60d;" FontFamily="pack://application:,,,/WpfApp3;component/Font/#iconfont" FontSize="100"></TextBlock>//备注:    字体文件的属性必须是资源。
复制代码

二、资源字典与样式

​ 基本语法:


//新建资源文件:Dictionary1.xaml//引用资源文件  <Window.Resources>        <ResourceDictionary>            <ResourceDictionary.MergedDictionaries>                <ResourceDictionary                     Source="pack://application:,,,/WpfApp3;component/Dictionary1.xaml"></ResourceDictionary>            </ResourceDictionary.MergedDictionaries>        </ResourceDictionary>    </Window.Resources> //备注:    ResourceDictionary.MergedDictionaries :合并资源文件     
复制代码


示例代码:


<Window x:Class="WpfApp3.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:WpfApp3"        mc:Ignorable="d"        Title="MainWindow" Height="450" Width="800">    <Window.Resources>        <ResourceDictionary>            <ResourceDictionary.MergedDictionaries>                <ResourceDictionary Source="pack://application:,,,/WpfApp3;component/Dictionary1.xaml"></ResourceDictionary>            </ResourceDictionary.MergedDictionaries>        </ResourceDictionary>    </Window.Resources>    <Grid>        <Button  Style="{StaticResource btn}"  Tag="1">            <!--<Button.Content>                     <TextBlock Text="Button"></TextBlock>             </Button.Content>-->        </Button>    </Grid></Window>  
复制代码


Dictionary1.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=System.Runtime">    <sys:String x:Key="text">Button</sys:String>    <Style TargetType="Button" x:Key="btn">        <Style.Setters>            <Setter Property="Width" Value="100"></Setter>            <Setter Property="Height" Value="100"></Setter>            <Setter Property="HorizontalAlignment" Value="Center"></Setter>            <Setter Property="VerticalAlignment" Value="Center"></Setter>            <Setter Property="BorderBrush" Value="Black"></Setter>            <Setter Property="BorderThickness"  Value="1"></Setter>            <Setter Property="Template">                <Setter.Value>                    <ControlTemplate>                        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" >                            <TextBlock Text="buttton" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}" x:Name="txt"></TextBlock>                        </Border>                        <ControlTemplate.Triggers >                            <Trigger Property="BorderThickness" Value="1">                                <Setter Property="FontSize" Value="30" TargetName="txt"></Setter>                            </Trigger>                        </ControlTemplate.Triggers>                    </ControlTemplate>                 </Setter.Value>              </Setter>           </Style.Setters>        <Style.Triggers>            <Trigger Property="Tag" Value="1">                <Setter Property="BorderThickness" Value="5"></Setter>                <Setter Property="BorderBrush" Value="Red"></Setter>            </Trigger>        </Style.Triggers>    </Style></ResourceDictionary> //备注:    Tag :设置标签值   可以存放任何值    TargetName:在控件模板中给哪个Name设置样式。 
复制代码

三、容器模板与数据模板

<Window x:Class="WpfApp3.Window1"        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:WpfApp3"        mc:Ignorable="d"        Title="Window1" Height="450" Width="800">    <Window.Resources>        <ResourceDictionary>            <ResourceDictionary.MergedDictionaries>                <ResourceDictionary Source="pack://application:,,,/WpfApp3;component/Dictionary2.xaml"></ResourceDictionary>             </ResourceDictionary.MergedDictionaries>        </ResourceDictionary>     </Window.Resources>    <Grid>        <ScrollViewer>            <ListView ItemsSource="{StaticResource per}"  AlternationCount="2">                 <!--容器样式-->                <ListView.ItemContainerStyle>                    <Style TargetType="ListViewItem">                        <Style.Triggers>                            <Trigger Property="ItemsControl.AlternationIndex"  Value="1">                                <Setter Property="Background" Value="Orange"></Setter>                            </Trigger>                        </Style.Triggers>                    </Style>                </ListView.ItemContainerStyle>                <!--样式选择器-->                <ListView.ItemTemplateSelector>                    <local:ListViewTemplateSelector NoTemp="{StaticResource NoTemp}"                                                     Temp="{StaticResource Temp}">                    </local:ListViewTemplateSelector>                </ListView.ItemTemplateSelector>                <ListView.ItemsPanel>                    <!--容器模板-->                    <ItemsPanelTemplate>                        <StackPanel></StackPanel>                    </ItemsPanelTemplate>                </ListView.ItemsPanel>             </ListView>        </ScrollViewer>    </Grid></Window>
备注: AlternationCount: 比如:值=2 已两个为一组进行循环。 ItemsControl.AlternationIndex: 比如:值=1 下标为1的进行样式设置。
复制代码


资源文件


<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"                    xmlns:local="clr-namespace:WpfApp3">    <x:Array Type="local:Person" x:Key="per">        <local:Person Name="小明1" Age="12" ID="1"></local:Person>        <local:Person Name="小明2" Age="12" ID="2"></local:Person>        <local:Person Name="小明3" Age="12" ID="3"></local:Person>        <local:Person Name="小明4" Age="12" ID="4"></local:Person>        <local:Person Name="小明5" Age="12" ID="5"></local:Person>        <local:Person Name="小明6" Age="12" ID="6"></local:Person>        <local:Person Name="小明7" Age="12" ID="7"></local:Person>        <local:Person Name="小明8" Age="12" ID="8"></local:Person>        <local:Person Name="小明9" Age="12" ID="9"></local:Person>        <local:Person Name="小明10" Age="13" ID="10"></local:Person>        <local:Person Name="小明11" Age="12" ID="11"></local:Person>     </x:Array>    <DataTemplate x:Key="NoTemp">        <Grid>            <Grid.RowDefinitions>                <RowDefinition></RowDefinition>            </Grid.RowDefinitions>            <Grid.ColumnDefinitions>                <ColumnDefinition></ColumnDefinition>                <ColumnDefinition></ColumnDefinition>                <ColumnDefinition></ColumnDefinition>            </Grid.ColumnDefinitions>            <TextBlock Text="{Binding ID}"></TextBlock>            <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Name}"></TextBlock>            <TextBlock Grid.Row="2" Grid.Column="2" Text="{Binding Age}" ></TextBlock>        </Grid>    </DataTemplate>    <DataTemplate x:Key="Temp">        <Grid TextBlock.Foreground="Red">            <Grid.RowDefinitions>                <RowDefinition></RowDefinition>            </Grid.RowDefinitions>            <Grid.ColumnDefinitions>                <ColumnDefinition></ColumnDefinition>                <ColumnDefinition></ColumnDefinition>                <ColumnDefinition></ColumnDefinition>            </Grid.ColumnDefinitions>            <TextBlock Text="{Binding ID}"></TextBlock>            <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Name}"></TextBlock>            <TextBlock Grid.Row="2" Grid.Column="2" Text="{Binding Age}" ></TextBlock>        </Grid>    </DataTemplate></ResourceDictionary>
复制代码


实体类文件


using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;
namespace WpfApp3{ public class Person { public int ID { get; set; } public string Name { get; set; } public int Age { get; set; } }}
复制代码


样式选择文件


using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows;using System.Windows.Controls;
namespace WpfApp3{ public class ListViewTemplateSelector:DataTemplateSelector { public DataTemplate NoTemp { get; set; } public DataTemplate Temp { get; set; }
/// <summary> /// 选择器 /// </summary> /// <param name="item">每个控件的子项对应的数据子项</param> /// <param name="container"></param> /// <returns>当前这个子项可使用的数据模板</returns> public override DataTemplate SelectTemplate(object item, DependencyObject container) { var per = (Person)item; if (per.Age == 13) { return Temp; } return NoTemp; } }}
复制代码


发布于: 刚刚阅读数: 2
用户头像

@@神农

关注

还未添加个人签名 2022.03.14 加入

好好学习,天天向上!

评论

发布
暂无评论
WPF -资源引用、资源字典引用以及容器模板和数据模板_@@神农_InfoQ写作平台