最近在捣鼓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之模板控件应用的更多相关文章

  1. 深入探讨WPF的ListView控件

    接上一篇博客初步探讨WPF的ListView控件(涉及模板.查找子控件)  我们继续探讨ListView的用法      一.实现排序功能 需求是这样的:假如我们把学生的分数放入ListView,当我 ...

  2. WPF中查找控件的扩展类

    在wpf中查找控件要用到VisualTreeHelper类,但这个类并没有按照名字查找控件的方法,于是搜索网络,整理出下面这个类,感觉用起来很是方便. 贴出来,供大家参考. /// <summa ...

  3. WPF范围选择控件(RangeSelector)

    原文:WPF范围选择控件(RangeSelector) 版权声明:本文为博主原创文章,转载请注明作者和出处 https://blog.csdn.net/ZZZWWWPPP11199988899/art ...

  4. 潜移默化学会WPF(难点控件treeview)--改造TreeView(CheckBox多选择版本),递归绑定数据

    原文:潜移默化学会WPF(难点控件treeview)--改造TreeView(CheckBox多选择版本),递归绑定数据 目前自己对treeview的感慨很多 今天先讲 面对这种 表结构的数据 的其中 ...

  5. WPF 4 DataGrid 控件(进阶篇一)

    原文:WPF 4 DataGrid 控件(进阶篇一)      上一篇<WPF 4 DataGrid 控件(自定义样式篇)>中,我们掌握了DataGrid 列表头.行表头.行.单元格相关的 ...

  6. WPF 4 DataGrid 控件(进阶篇二)

    原文:WPF 4 DataGrid 控件(进阶篇二)      上一篇<WPF 4 DataGrid 控件(进阶篇一)>中我们通过DataGridTemplateColumn 类自定义编辑 ...

  7. 示例:WPF中Slider控件封装的缓冲播放进度条控件

    原文:示例:WPF中Slider控件封装的缓冲播放进度条控件 一.目的:模仿播放器播放进度条,支持缓冲任务功能 二.进度: 实现类似播放器中带缓存的播放样式(播放区域.缓冲区域.全部区域等样式) 实现 ...

  8. WPF中TreeView控件数据绑定和后台动态添加数据(一)

    数据绑定: 更新内容:补充在MVVM模式上的TreeView控件数据绑定的代码. xaml代码: <TreeView Name="syntaxTree" ItemsSourc ...

  9. WPF中Ribbon控件的使用

    这篇博客将分享如何在WPF程序中使用Ribbon控件.Ribbon可以很大的提高软件的便捷性. 上面截图使Outlook 2010的界面,在Home标签页中,将所属的Menu都平铺的布局,非常容易的可 ...

随机推荐

  1. putty ssh login linux

    (1) in linux $ ssh-keygen -t dsa $ cd .ssh $ cat id_dsa.pub > authorized_keys $ chmod 600 authori ...

  2. Java Script基础(八) Array数组对象

    一.Array数组 JavaScript中的数组也是具有相同数据类型的一个或者多个值得集合.用法和Java中的数组类似. Array对象的常用属性和方法: 属性: length:获取数组的长度: 方法 ...

  3. Delphi数组复制

    const AA : arrary[0..4] of byte =(0,1,2,3,4) var BB : arrary[0..4] of byte; begin BB := AA ;   {这样是错 ...

  4. Arduino

    ========================================= Sites/Blogs http://arduino.cc/ http://www.geek-workshop.co ...

  5. HTML5+CSS3-机器猫

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. bootstrap daterangepicker 添加阴历及节假日

    所有的新增都用 'myAdd start'和'myAdd end'标注,所有的修改都用'myChange start'和'myChange end'标注. 借用了 1900-2100区间内的公历.农历 ...

  7. 【转载】Android开发学习笔记:Intent的简介以及属性的详解

    http://liangruijun.blog.51cto.com/3061169/634411/ 一.Intent的介绍 Intent的中文意思是“意图,意向”,在Android中提供了Intent ...

  8. CefSharp 发布后在客户机上报找不到dll的问题

    两个因素:一是与项目平台属性的选择要一致二是需要安装CefSharp对应的的VC++可发行组件包(用包管理器引用了此DLL后,会有一个readme.txt,上面详细介绍了CefSharp所需要的环境要 ...

  9. Part 16 Important concepts related to functions in sql server

    Important concepts related to functions in sql server

  10. RAC监听与tns

    监听: 个人理解:本来想通过scan ip来配置tns总数报12545,后来通过vip来配置tns 11g rac的监听在安装时创建,由grid用户管理监听,listener.ora文件在$ORACL ...