ControlTemplate

  1. ControlTemplate:用于定义控件的结构和外观,这样可以将控件外观与控件功能分离开. 在xaml中ControlTemplate通常配置到Style中,通过Style控制控件的结构和外观
  2. 如果控件继承自ContentControl类,其模板将包含一个ContentPresenter类成员,ContentPresenter用于指定添加内容的位置,下面是一个Button Style,ContentPresenter的标记表明Button的Content应在Grid内垂直且水平居中显示
    ContentPresenter example<Style TargetType="Button">
    <!--Set to true to not get any properties from the themes.-->
    <Setter Property="OverridesDefaultStyle" Value="True"/>
    <Setter Property="Template">
    <Setter.Value>
    <ControlTemplate TargetType="Button">
    <Grid>
    <Ellipse Fill="{TemplateBinding Background}"/>
    <ContentPresenter HorizontalAlignment="Center"
    VerticalAlignment="Center"/>
    </Grid>
    </ControlTemplate>
    </Setter.Value>
    </Setter>
    </Style>
  3. ControlTemplate中也可以添加Trigger,当一个属性发生变化时用于控制一个或者多个属性的变化。如下代码,注意TargetName属性指定了control的名字,满足条件时就会修改该control的属性
    ControlTemplate example<ControlTemplate TargetType="ListBoxItem">
    <Border Name="ItemBorder" BorderThickness="1" Margin="1" Background="{StaticResource TransparentBrush}">
    <ContentPresenter />
    </Border>
    <ControlTemplate.Triggers>
    <Trigger Property="IsSelected" Value="True">
    <Setter TargetName="ItemBorder" Property="Background" Value="{StaticResource TransparentBrush}" />
    </Trigger>
    <Trigger Property="IsMouseOver" Value="True">
    <Setter TargetName="ItemBorder" Property="Background" Value="{StaticResource TransparentBrush}" />
    </Trigger>
    </ControlTemplate.Triggers>
    </ControlTemplate>
  4. 如果控件继承自ItemsControl类,其模板将包含一个ItemsPresenter元素,指示何处放置列表项的模板
  5. 模板绑定:通过使用模板绑定,模板可以从应用模板的控件提取一个值,ContentPresenter之所以能显示Content,就是因为它有一个隐式的模板绑定将ContentPresenter.Content设置为Button.Content,如下代码则设置button content的内边距,如果不把Margin通过TemplateBinding绑定到Padding属性,Button的content 会与侧边重合
    TemplateBinding example<ControlTemplate x:key="ButtonTemplate" TargetType="{x:Type Button}">
    <Border BorderBrush="Orange" BorderThickness="3" CornerRadius="2"
    Background="Red" TextBlock.Foreground="White">
    <ContentPresenter RecognizeAccessKey="True"
    Margin="{TemplateBinding Padding}">
    </ContentPresenter>
    </Border>
    </ControlTemplate>
  6. 模板与样式:两者都可以改变control的外观,但样式被限制在一定范围,它不能使用由不同子control组成的可视化数替换control的原有外观,如条目2中的椭圆形按钮,仅使用style无法实现.
  7. WPF编程宝典中提供的能查看每个control的Template的工具,关键代码如下:
    TemplateBinding exampleprivate void Window_Loaded(object sender, RoutedEventArgs e)
    {
    Type controlType = typeof(Control);
    List<Type> derivedTypes = new List<Type>();
    Assembly assembly = Assembly.GetAssembly(typeof(Control));
    foreach (Type type in assembly.GetTypes())
    {
    if (type.IsSubclassOf(controlType) && !type.IsAbstract && type.IsPublic)
    {
    derivedTypes.Add(type);
    }
    }
    derivedTypes.Sort(delegate(Type x, Type y)
    {
    return x.FullName.CompareTo(y.FullName);
    });
    lstTypes.ItemsSource = derivedTypes;
    }
    private void lstTypes_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
    try
    {
    Type type = (Type)lstTypes.SelectedItem;
    ConstructorInfo info = type.GetConstructor(Type.EmptyTypes);
    Control control = (Control)info.Invoke(null);
    control.Visibility = Visibility.Collapsed;
    grid.Children.Add(control);
    ControlTemplate template = control.Template;
    XmlWriterSettings setting = new XmlWriterSettings();
    setting.Indent = true;
    StringBuilder sb = new StringBuilder();
    XmlWriter writer = XmlWriter.Create(sb, setting);
    System.Windows.Markup.XamlWriter.Save(template, writer);
    txtTemplate.Text= sb.ToString();
    grid.Children.Remove(control);
    }
    catch (Exception ex)
    {
    txtTemplate.Text = string.Format("Error generating tempalte:{0}",ex);
    }
    }

DataTemplate


  1. DataTemplate支持对数据的表现形式进行自定义,它可以包含任何元素的组合,还应当包含一个或者多个数据绑定表达式,有两种类型的控件支持:
    • 内容控件,通过ContentTemplate属性支持数据模板,内容模板用于显示任何放在Content属性中的内容
    • 列表控件,继承自ItemsControl的控件,通过ItemTemplate属性支持数据模板,用于显示ItemsSource提供的集合中的每个项。也就是该模板会被用作列表中每个项的ContentTemplate模板,如对于ListBox控件,数据模板会作用与ListBoxItem元素,而对于ComboBox则会作用于ComboBoxItem元素
  2. DataTemplate支持使用 DataTemplate.DataType来确定使用模板的绑定数据的类型
  3. 使用下列技术可以使DataTemplate更灵活的表现数据
    • DataTrigger:可以根据绑定的数据对象中的属性值使用触发器修改模板中的属性

      DataTrigger example<DataTemplate x:Key="DefaultTemplate">
      <DataTemplate.Triggers>
      <DataTrigger Binding="{Binding Path=CategoryName}" Value="Tools">
      <Setter Property="ListBoxItem.Foreground" Value="Red"></Setter>
      </DataTrigger>
      </DataTemplate.Triggers>
      </DataTemplate>
    • 使用值转换器:实现了IValueConverter接口的类,能够将一个值从绑定的对象转换为可用于设置模板中与格式化相关的属性的值
      IValueConverter example<DataTemplate x:Key="DefaultTemplate">
      <Image Source="{Binding path=imagePath}" Converters="{StaticResource ImagePathConverter}" />
      </DataTemplate>
    • 使用模板选择器:模板选择器检查绑定的数据对象,并在几个不同的模板之间进行选择。需要继承DataTemplateSelector类,重写SelectTemplate函数。如下代码自定义的SingleHighlightTemplateSelector类包含连个数据模板,并根据属性的值在SelectTemplate函数中选择相应的数据模板
      DataTemplateSelector example<ListBox Name="lsProducts" HorizontalContentAlignment="Stretch">
      <ListBox.ItemTemplateSelector>
      <local:SingleHighlightTemplateSelector
      DefaultTemplate="{StaticResource DefaultTemplate}"
      HighlightTemplate="{StaticResource HighlighTemplate}"
      PropertyoEvaluate="CategoryName"
      PropertyValueToHighlight="Travel"></local:SingleHighlightTemplateSelector>
      </ListBox.ItemTemplateSelector>
      </ListBox>

WPF Template的更多相关文章

  1. WPF Template模版之DataTemplate与ControlTemplate【一】

    WPF Template模版之DataTemplate与ControlTemplate[一] 标签: Wpf模版 2015-04-19 11:52 510人阅读 评论(0) 收藏 举报  分类: -- ...

  2. WPF Template模版之寻找失落的控件【三】

    “井水不犯河水”常用来形容两个组织之间界限分明.互不相干,LogicTree与控件内部这颗小树之间就保持着这种关系.换句话说,如果UI元素树上有个X:Name=“TextBox1”的控件,某个控件内部 ...

  3. WPF Template模版之DataTemplate与ControlTemplate的关系和应用【二】

    1. DataTemplate和ControlTemplate的关系 学习过DataTemplate和ControlTemplate,你应该已经体会到,控件只是数据的行为和载体,是个抽象的概念,至于它 ...

  4. 【转】WPF Template模版之DataTemplate与ControlTemplate的关系和应用(二)

    1. DataTemplate和ControlTemplate的关系 学习过DataTemplate和ControlTemplate,你应该已经体会到,控件只是数据的行为和载体,是个抽象的概念,至于它 ...

  5. 【转】WPF Template模版之DataTemplate与ControlTemplate(一)

    WPF系统不但支持传统的Winfrom编程的用户界面和用户体验设计,更支持使用专门的设计工具Blend进行专业设计,同时还推出了以模板为核心的新一代设计理念. 1. 模板的内涵 作为表现形式,每个控件 ...

  6. wpf template的code写法

    this.Template = XamlReader.Load ("<ControlTemplate xmlns='http://schemas.microsoft.com/clien ...

  7. .NET: WPF Template

    Data Template: 要做一个listBox,里面有车子的简单信息,点了里面的item后就会显示详细信息. car class: using System; using System.Coll ...

  8. WPF:在ControlTemplate中使用TemplateBinding

    A bit on TemplateBinding and how to use it inside a ControlTemplate. Introductio Today I'll try to w ...

  9. WPF学习(10)模板

    在前面一篇我们粗略说了Style和Behaviors,如果要自定义一个个性十足的控件,仅仅用Style和Behaviors是不行的,Style和Behaviors只能通过控件的既有属性来简单改变外观, ...

随机推荐

  1. windows bat更改系统时间 & 同步internet时间

    最近刚好用到一款软件学习版,30天试用期满,但是还想继续用,不具体透露是什么软件了. 发现更改了系统时间后,这个软件就又能启动了,不过每次改来改去很麻烦,就写了段脚本 @echo off date 1 ...

  2. 使用 async/ await 进行 异步 编程

    一.异步函数 异步函数概念. 通常 是 指用 async 修饰 符 声明 的, 可 包含 await 表达式 的 方法 或 匿名 函数 1. 从 语言 的 视角 来看, 这些 await 表达式 正是 ...

  3. VB入门在线视频教程大全学习

    课程目录 9分钟47秒 课时1第一课:怎么编写程序 14分钟1秒 课时1第十七课第1节:文件读写的几种方式 14分钟14秒 课时2第二课:什么是变量和变量类型 19分钟24秒 课时3第三课:变量的声明 ...

  4. Front End Developer Questions

    Front End Developer Questions 感谢大神分享- 目录 前言 HTML部分 CSS部分 JavaScript部分 其他问题 前端学习网站推荐 前言 前言 HTML Docty ...

  5. 谈 instanceof 和 typeof 的实现原理

    typeof: js 在底层存储变量的时候,会在变量的机器码的低位1-3位存储其类型信息

  6. 编写高性能的javascript代码(持续更新)

    参考资料: Vanilla JS——世界上最轻量的JavaScript框架(没有之一) http://segmentfault.com/a/1190000000355277 探索高效jQuery的奥秘 ...

  7. Module build failed: Module failed because of a eslint warning

    eslint 设置 warning 级别,在 开发编译失败的原因,报错如下: F:\vue-mobile-skeleton>npm run dev > byhealth@1.0.0 dev ...

  8. HTML特殊符号对照表、常用的字符实体

    来源:http://tool.xker.com/htmlchar.php 最常用的字符实体 显示结果 描述 实体名称 实体编号   空格     < 小于号 < < > 大于号 ...

  9. CF487E Tourists(圆方树+堆+链剖)

    本题解并不提供圆方树讲解. 所以不会圆方树的出门右转问yyb 没有修改的话圆方树+链剖. 方点的权值为点双连通分量里的最小值. 然后修改的话圆点照修,每一个方点维护一个小根堆. 考虑到可能被菊花卡死. ...

  10. CF508E (贪心+搜索+构造)

    题目大意:让你构造一个括号序列,括号匹配的方式类似于栈,给出从左数每个括号 到和它匹配的右括号的 最小和最大距离,让你输出一个合法括号序列 看错题了以为是二分图,然后写了搜索 贪心发现如果距离往小了填 ...