ItemTemplateSelector
ItemTemplateSelector的中文翻译是模板选择器
是用来选择模板。
他的用法稍有不同,他必须派生于DataTemplateSelector类。
然后重写SelectTemplate这个方法,方法内由两个参数,一是对象所绑定的数据,二是你绑定的元素。方法有返回类型为DataTemplate的,不过默认值为Null
xaml则是通过绑定的方式。
模板选择的用比较广。
这里就介绍默认的使用方式。
通过数据或者对象元素来选择模板
选择器类
using System.Windows;
using System.Windows.Controls; namespace WinMenu
{
public class Select: DataTemplateSelector
{
private int i = ;
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
var u = container as FrameworkElement; i++; if (i % == )
return u.FindResource("d1") as DataTemplate;
else
return u.FindResource("d2") as DataTemplate; }
}
}
xaml代码:
<Window.Resources>
<local:Select x:Key="sl2"/> <DataTemplate x:Key="d1">
<Image x:Name="image" Height="" Width="" Source="{Binding Image}" />
</DataTemplate>
<DataTemplate x:Key="d2">
<Image x:Name="image" Height="" Width="" Source="{Binding Image}" />
</DataTemplate>
<Storyboard x:Key="S2">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" >
<EasingDoubleKeyFrame KeyTime="" Value=""/>
<EasingDoubleKeyFrame KeyTime="" Value=""/>
<EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="-5"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value=""/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" >
<EasingDoubleKeyFrame KeyTime="" Value=""/>
<EasingDoubleKeyFrame KeyTime="" Value=""/>
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value=""/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" >
<EasingDoubleKeyFrame KeyTime="" Value=""/>
<EasingDoubleKeyFrame KeyTime="" Value=""/>
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value=""/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
<Grid>
<ListBox ItemTemplateSelector="{StaticResource sl2}" x:Name="ListBoxFile" Margin="0,0,0,119" >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="RenderTransform" >
<Setter.Value>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Setter.Value>
</Setter>
<Style.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard Storyboard="{StaticResource S2}"/>
</EventTrigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
<Button Click="Button_Click" Margin="451,321,0,0"/> </Grid>
图片

ItemTemplateSelector的更多相关文章
- UWP开发入门(八)——聊天窗口和ItemTemplateSelector
我们平常用的最多的APP可能就是企鹅和微信了.有没有想过聊天窗口如何实现的?本篇我们将简单模拟一个聊天窗口. 聊天窗口大致上就是消息的一个集合列表.集合列表最常见的展现形式无非就是ListView.可 ...
- wpf Listbox 设置ItemContainerStyle后,ItemTemplateSelector不能触发
解决方案: 将Listbox 的ItemTemplateSelector 改为 ItemContainerStyle中ContentPresenter ContentTemplateSelector ...
- 项模板选择器属性(ItemTemplateSelector属性)和样式选择器(ItemContainerStyleSelector)
3.4.5 共享尺寸组 样式选择器: 或者========================================
- wpf ListView DataTemplate方式的鼠标悬停和选中更改背景色
今天使用wpf技术弄一个ListView的时候,由于需求需要,需要ListView显示不同的数据模板,很自然的使用了DataTemplate方式来定义多个数据模板,并在ListView中使用ItemT ...
- WPF Telerik TreeListView样式设计
Telerik控件 TreeListView 修改其中样式 1.添加TreeListView控件 <telerik:RadTreeView x:Name="ObjecTreeView& ...
- [WPF系列]-数据邦定之DataTemplate 对 ItemsControl 进行样式和模板处理
引言 即使 ItemsControl 不是 DataTemplate 所用于的唯一控件类型,将 ItemsControl 绑定到集合仍然很常见. 在 DataTemplate 中有哪些内容一节中, ...
- [WPF系列]-数据邦定之DataTemplate 根据对象属性切换模板
引言 书接上回[WPF系列-数据邦定之DataTemplate],本篇介绍如何根据属性切换模板(DataTemplate) 切换模板的两种方式: 使用DataTemplateSelecto ...
- WPF Combobox样式
<ControlTemplate x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}&qu ...
- UWP开发随笔——UWP新控件!AutoSuggestBox!
摘要 要开发一款优秀的application,控件肯定是必不可少的,uwp就为开发者提供了各种各样的系统控件,AutoSuggestBox就是uwp极具特色的控件之一,也是相对于之前win8.1的ua ...
随机推荐
- 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 17—Large Scale Machine Learning 大规模机器学习
Lecture17 Large Scale Machine Learning大规模机器学习 17.1 大型数据集的学习 Learning With Large Datasets 如果有一个低方差的模型 ...
- mysql varchar类型转换int类型
select * from gyzd_yysinfo order by cast(yysid as SIGNED INTEGER) 或者 select * from gyzd_yysinfo orde ...
- 返回一个可用的名称如1_4.txt
); MessageBox.Show(tt); }
- git 转移
git push --mirror https://github.com/cloud-pi/drbd-docker-plugin.git
- Unity iOS 项目的一种性能评测方法
[Unity iOS 项目的一种性能评测方法]
- FP寻源报错
ERROR [HY000] [Oracle][ODBC][Ora]ORA-01114: IO error writing block to file (block # ) ORA-01114: I ...
- 01 lucene基础 北风网项目培训 Lucene实践课程 系统架构
Lucene在搜索的时候数据源可以是文件系统,数据库,web等等. Lucene的搜索是基于索引,Lucene是基于前面建立的索引之上进行搜索的. 使用Lucene就像使用普通的数据库一样. Luce ...
- cdoj31-饭卡(card) (01背包)
http://acm.uestc.edu.cn/#/problem/show/31 饭卡(card) Time Limit: 3000/1000MS (Java/Others) Memory ...
- OSG QT
https://blog.csdn.net/a_Treasure/article/details/82152245 https://www.bbsmax.com/A/kPzOQ4oo5x/ https ...
- arping
强制交换机刷新MAC arping -I em2(网卡名称) 58.215.88.8(Vip)