【笔记】WPF之模板控件应用
最近在捣鼓WPF的动画,想自定义一个控件模型来实现动画。
目标功能是这样:在WPF项目文件中创建一个自定义用户控件模型,该模型最外层是一个Grid,Grid布局为3行1列,第一列是一个图片按钮,第二列为主标题,第三列为副标题,XAML语句如下:
<Grid Name="grid" Background="Transparent">
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="225"></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Button Name="btn" BorderThickness="0" BorderBrush="Transparent" Background="Transparent">
<Button.Template>
<ControlTemplate>
<!--定义视觉树-->
<Image Name="img" Width="{TemplateBinding Button.Width}" Height="{TemplateBinding Button.Height}" Source="{TemplateBinding Button.Content}"></Image>
<!--定义视觉树-->
</ControlTemplate>
</Button.Template>
</Button>
<TextBlock Name="title" FontSize="24" Foreground="Blue" HorizontalAlignment="Center" Text="Title" Grid.Column="0" Grid.Row="1"></TextBlock>
<TextBlock Name="subtitle" FontSize="24" Foreground="Blue" HorizontalAlignment="Center" Text="Subtitle" Grid.Column="0" Grid.Row="2"></TextBlock>
</Grid>
注意到在第一列图片按钮处添加了控件模板,并将Image高和宽绑定到按钮的高和宽,图片源绑定到Button的Content属性上。
后台C#代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace HordeElves
{
/// <summary>
/// MyButton.xaml 的交互逻辑
/// </summary>
public partial class MyFirstButton : UserControl
{
public MyFirstButton()
{
InitializeComponent();
}
}
}
这样,我只要在需要按钮的地方实例化一个MyFirstButton的实例,并给其Content属性附上一个ImageSource对象即可。
但在做的过程中遇到了问题,我给我Button实例的Width和Height属性赋值都可以绑定到自定义控件的属性上,但给Content赋值时出现了问题,控件不显示。
btn = new MyFirstButton();
btn.Title = "我是主标题";
btn.SubTitle = "我是副标题";
btn.Content = new BitmapImage(new Uri("../../Resources/icon01.png", UriKind.Relative)) as ImageSource;
试着对代码进行修改:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace HordeElves
{
/// <summary>
/// MyButton.xaml 的交互逻辑
/// </summary>
public partial class MyFirstButton : UserControl
{
/// <summary>
/// 按钮的主标题
/// </summary>
public string Title
{
get { return this.title.Text; }
set { this.title.Text = value; }
}
/// <summary>
/// 按钮的副标题
/// </summary>
public string SubTitle
{
get { return this.subtitle.Text; }
set { this.subtitle.Text = value; }
}
/// <summary>
/// 按钮的图片源
/// </summary>
public ImageSource Source
{
get { return (ImageSource)this.btn.Content; }
set { this.btn.Content = value; }
} public MyFirstButton()
{
InitializeComponent();
}
}
}
以上是对几个特别的参数进行了封装,当我重新对Source属性赋值,就能够正常显示了。
btn = new MyFirstButton();
btn.Title = "我是主标题";
btn.SubTitle = "我是副标题";
btn.Source = new BitmapImage(new Uri("../../Resources/icon01.png", UriKind.Relative)) as ImageSource;
这里的问题在于,Xaml语句中Image的Source属性对应的是一个ImageSource对象,而Button的Content属性是一个object对象,修改的意义在于对其进行了兼容转换,这样Image的Source才能识别Content所含有的内容,记于此,供日后查证。
【笔记】WPF之模板控件应用的更多相关文章
- 深入探讨WPF的ListView控件
接上一篇博客初步探讨WPF的ListView控件(涉及模板.查找子控件) 我们继续探讨ListView的用法 一.实现排序功能 需求是这样的:假如我们把学生的分数放入ListView,当我 ...
- WPF中查找控件的扩展类
在wpf中查找控件要用到VisualTreeHelper类,但这个类并没有按照名字查找控件的方法,于是搜索网络,整理出下面这个类,感觉用起来很是方便. 贴出来,供大家参考. /// <summa ...
- WPF范围选择控件(RangeSelector)
原文:WPF范围选择控件(RangeSelector) 版权声明:本文为博主原创文章,转载请注明作者和出处 https://blog.csdn.net/ZZZWWWPPP11199988899/art ...
- 潜移默化学会WPF(难点控件treeview)--改造TreeView(CheckBox多选择版本),递归绑定数据
原文:潜移默化学会WPF(难点控件treeview)--改造TreeView(CheckBox多选择版本),递归绑定数据 目前自己对treeview的感慨很多 今天先讲 面对这种 表结构的数据 的其中 ...
- WPF 4 DataGrid 控件(进阶篇一)
原文:WPF 4 DataGrid 控件(进阶篇一) 上一篇<WPF 4 DataGrid 控件(自定义样式篇)>中,我们掌握了DataGrid 列表头.行表头.行.单元格相关的 ...
- WPF 4 DataGrid 控件(进阶篇二)
原文:WPF 4 DataGrid 控件(进阶篇二) 上一篇<WPF 4 DataGrid 控件(进阶篇一)>中我们通过DataGridTemplateColumn 类自定义编辑 ...
- 示例:WPF中Slider控件封装的缓冲播放进度条控件
原文:示例:WPF中Slider控件封装的缓冲播放进度条控件 一.目的:模仿播放器播放进度条,支持缓冲任务功能 二.进度: 实现类似播放器中带缓存的播放样式(播放区域.缓冲区域.全部区域等样式) 实现 ...
- WPF中TreeView控件数据绑定和后台动态添加数据(一)
数据绑定: 更新内容:补充在MVVM模式上的TreeView控件数据绑定的代码. xaml代码: <TreeView Name="syntaxTree" ItemsSourc ...
- WPF中Ribbon控件的使用
这篇博客将分享如何在WPF程序中使用Ribbon控件.Ribbon可以很大的提高软件的便捷性. 上面截图使Outlook 2010的界面,在Home标签页中,将所属的Menu都平铺的布局,非常容易的可 ...
随机推荐
- centos 下 yum安装和卸载软件
安装的命令是,yum install xxx,yum会查询数据库,有无这一软件包,如果有,则检查其依赖冲突关系,如果没有依赖冲突,那么最好,下载安装;如果有,则会给出提示,询问是否要同时安装依赖,或删 ...
- 【MongoDB】增删改查基本操作
查看所有数据库 show dbs 切换数据库(若不存在,会自动创建) use databasename 删除当前数据库 db.dropDatabase() MongoDB中没有表,只有集合. 插入集合 ...
- hdu-5714 拍照(二分)
题目链接: 拍照 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Proble ...
- hdu 4739 状压DP
这里有状态压缩DP的好博文 题目:题目比较神,自己看题目吧 分析: 大概有两种思路: 1.dfs,判断正方形的话可以通过枚举对角线,大概每次减少4个三角形,加上一些小剪枝的话可以过. 2.状压DP,先 ...
- Linux Centos 怎么安装更新根证书实现支持https访问
其实很简单就是一个命令: mozroots --import --ask-remove 或者使用: sudo update-ca-certificates
- 在phalcon框架下,php接口规范以及接口实例
接口规范实例 前言 由于本人也是第一次写接口,之前对于接口也是一知半解,没有系统的了解过,所以这次也是写的自己的在这几天在APP项目中关于接口的浅层次的认识,如果有不妥或者不当的地方还请指出,再此谢谢 ...
- [转]JAVA三大框架SSH和MVC
Java—SSH(MVC) JAVA三大框架的各自作用 hibernate是底层基于jdbc的orm(对象关系映射)持久化框架,即:表与类的映射,字段与属性的映射,记录与对象的映射 数据库模型 也就 ...
- leetcode题1Two sum 练习
题目为: 给一个整数数组, 返回数组中的两数之和等于指定值的两数在数组中的下标. Example: Given nums = [2, 7, 11, 15], target = 9, Because n ...
- Java学习之Java的单例模式
单例模式有一下特点: 1.单例类只能有一个实例.2.单例类必须自己自己创建自己的唯一实例.3.单例类必须给所有其他对象提供这一实例. 单例模式确保某个类只有一个实例,而且自行实例化并向整个系统提供这个 ...
- JAVA:类的三大特征,抽象类,接口,final关键字<3>
一.类的三大特征 1.封装性 (1).什么是封装 封装就是把抽象出的数据和对数据的操作封装在一起, 数据被保护在内部, 程序的其他部分只有通过被授权的操作(成员方法), 才能对数据进行操作. (2). ...