最近在捣鼓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. 【Mood-5】14条建议,使你的IT职业生涯更上一层楼

    升值为企业IT部门的领导者,并非一件易事.从一般大众中脱颖而出,则更不容易. 2013是一个好年头,据专家报告显示,6月所有新工作中,10%来自技术领域.这对于那些希望高升.换岗.跳槽的IT技术人员来 ...

  2. 用pf透明地将流量从一台机器转到另一台机器上的缘起及实现方式对比

    下面是也是我在12580工作时发生的事情,重新记录并发出来.这种特殊需求很考 验PF的功底.在新旧系统并存,做重构的时候有时很需要这种救急的作法.一.缘起miscweb1(172.16.88.228) ...

  3. 转:Android官方MVP架构示例项目解析

    转自: http://www.infoq.com/cn/articles/android-official-mvp-architecture-sample-project-analysis 作者 吕英 ...

  4. 转: ImageMagick 命令行的图片处理工具(客户端与服务器均可用)

    http://www.imagemagick.com.cn/ 关于ImageMagick ImageMagick (TM) 是一个免费的创建.编辑.合成图片的软件.它可以读取.转换.写入多种格式的图片 ...

  5. MongoDB - The mongo Shell, Access the mongo Shell Help

    In addition to the documentation in the MongoDB Manual, the mongo shell provides some additional inf ...

  6. 移动开发Html 5前端性能优化指南

    详细内容请点击 PC优化手段在Mobile侧同样适用在Mobile侧我们提出三秒种渲染完成首屏指标基于第二点,首屏加载3秒完成或使用Loading基于联通3G网络平均338KB/s(2.71Mb/s) ...

  7. 代码研磨 Slim v3 (一)--app->get()&route->add()

    index.php代码如下: $app->get('/forbase', function ($request, $response, $args){ Example\Module\Base:: ...

  8. 一.CSS工作原理

    CSS全称层叠样式表,它是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言.是能够真正做到网页表现与内容分离的一种样式设计语言,能够对网页 ...

  9. CI框架程序--本地调试之后部署新浪SAE

    前几天给朋友写了个简单的网站, 想想还是部署到服务器上让朋友看一下效果! 用CI框架写的,有个SAE新浪云的账号,可以用!就部署到上面去了!途中遇到了一些问题!一一解决了! 在这里分享一下 供遇到这些 ...

  10. webkit常见问题汇总

    前段时间有人问我一个简单的问题,html如何创建解析的? 我讲了一大堆,什么通过DocumentLoader, CachedResourceLoader, CacheResource, Resourc ...