资源引用、资源字典引用以及容器模板和数据模板
2022/1/11 23:04:53
本文主要是介绍资源引用、资源字典引用以及容器模板和数据模板,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
一、资源引用
语法:
//基本语法 pack://application:,,,/项目名称;component/文件名称 //引用字体 <TextBlock Text="" 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; } } }
这篇关于资源引用、资源字典引用以及容器模板和数据模板的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-10百万架构师第十三课:源码分析:Spring 源码分析:Spring核心IOC容器及依赖注入原理|JavaGuide
- 2025-01-10便捷好用的电商API工具合集
- 2025-01-09必试!帮 J 人团队解决物流错发漏发的软件神器!
- 2025-01-09不容小觑!助力 J 人物流客服安抚情绪的软件!
- 2025-01-09为什么医疗团队协作离不开智能文档工具?
- 2025-01-09惊叹:J 人团队用啥软件让物流服务快又准?
- 2025-01-09如何利用数据分析工具优化项目资源分配?4种工具推荐
- 2025-01-09多学科协作难?这款文档工具可以帮你省心省力
- 2025-01-09团队中的技术项目经理TPM:工作内容与资源优化策略
- 2025-01-09JIT生产管理法:优化流程,提升竞争力的秘诀