Silverlight 用DependencyProperty 自定义ImageButton控件 定义属性
为ImageButton自定义IconSource和Contents属性
xaml代码
<UserControl x:Class="SilverlightCreate.SilverlightButtons"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="30" d:DesignWidth="100"> <Grid x:Name="LayoutRoot" Background="Transparent">
<StackPanel x:Name="myButton" Orientation="Horizontal" >
<Image x:Name="myImg" Stretch="None" />
<TextBlock x:Name="myText" VerticalAlignment="Center" FontSize="13" Padding="5" />
</StackPanel>
<Rectangle x:Name="myRectangle" Margin="-3" />
</Grid>
</UserControl>
下面开始自定义属性内容,自定义属性要用 依赖属性类 DependencyProperty
public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.Register("MyProperty", typeof(int), typeof(ownerclass), new UIPropertyMetadata(0));
DependencyProperty 的Register 方法中有四个参数,第一个是自定的属性,第二个自定义属性的参数类型,第三个是自定义属性所属类,第四个是属性元数据的实例,参数类型是PropertyMetadata。
使用vs2010的小技巧,生成依赖属性可以输入propdp,然后按两下Tab键,就会自动生成如下代码

cs代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging; namespace SilverlightCreate
{
public partial class SilverlightButtons : UserControl
{
public SilverlightButtons()
{
InitializeComponent();
} /// <summary>
/// 自定义控件文本
/// </summary>
public string Contents
{
get { return (string)GetValue(ContentsProperty); }
set { SetValue(ContentsProperty, value); }
} /// <summary>
/// 自定义控件图片
/// </summary>
public ImageSource IconSource
{
get { return (ImageSource)GetValue(IconSourceProperty); }
set { SetValue(IconSourceProperty, value); }
} /// <summary>
/// 自定义控件背景色
/// </summary>
public Brush ButtonBackGround
{
get { return (SolidColorBrush)GetValue(ButtonBackGroundProperty); }
set { SetValue(ButtonBackGroundProperty, value); }
} /// <summary>
/// 自定义控件文字颜色
/// </summary>
public Brush FontColor
{
get { return (Brush)GetValue(FontColorProperty); }
set { SetValue(FontColorProperty, value); }
} /// <summary>
/// 自定义控件边框默认颜色
/// </summary>
public Brush DefaultStroke
{
get { return (Brush)GetValue(DefaultStrokeProperty); }
set { SetValue(DefaultStrokeProperty, value); }
} /// <summary>
/// 自定义控件边框高亮颜色
/// </summary>
public Brush HighLightStroke
{
get { return (Brush)GetValue(HighLightStrokeProperty); }
set { SetValue(HighLightStrokeProperty, value); }
} /// <summary>
/// 自定义控件填充默认颜色
/// </summary>
public Brush DefaultFill
{
get { return (Brush)GetValue(DefaultFillProperty); }
set { SetValue(DefaultFillProperty, value); }
} /// <summary>
/// 自定义控件填充高亮颜色
/// </summary>
public Brush HighLightFill
{
get { return (Brush)GetValue(HighLightFillProperty); }
set { SetValue(HighLightFillProperty, value); }
} /// <summary>
/// 自定义控件边框厚度
/// </summary>
public double StrokeThickness
{
get { return (double)GetValue(StrokeThicknessProperty); }
set { SetValue(StrokeThicknessProperty, value); }
} /// <summary>
/// 自定控件边框圆角x
/// </summary>
public double RadiusX
{
get { return (double)GetValue(RadiusXProperty); }
set { SetValue(RadiusXProperty, value); }
} /// <summary>
/// 自定控件边框圆角y
/// </summary>
public double RadiusY
{
get { return (double)GetValue(RadiusYProperty); }
set { SetValue(RadiusYProperty, value); }
} public static readonly DependencyProperty ContentsProperty =
DependencyProperty.Register("Contents", typeof(string), typeof(SilverlightButtons), new PropertyMetadata(ContentsChanged)); private static void ContentsChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
SilverlightButtons button = sender as SilverlightButtons;
if (button != null)
{
if (e.NewValue != null)
{
String NewValue = e.NewValue as String;
button.myText.Text = NewValue;
}
}
else
{
button.myText.Text = String.Empty;
}
} public static readonly DependencyProperty IconSourceProperty =
DependencyProperty.Register("IconSource", typeof(ImageSource), typeof(SilverlightButtons), new PropertyMetadata(IconSourceSourceChanged)); private static void IconSourceSourceChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
SilverlightButtons button = sender as SilverlightButtons;
if (button != null)
{
if (e.NewValue != null)
{
ImageSource source = e.NewValue as ImageSource;
button.myImg.Source = source;
}
}
else
{
button.myImg.Source = null;
}
} public static readonly DependencyProperty ButtonBackGroundProperty =
DependencyProperty.Register("ButtonBackGround", typeof(Brush), typeof(SilverlightButtons), new PropertyMetadata(ButtonBackGroundChanged)); private static void ButtonBackGroundChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
SilverlightButtons button = sender as SilverlightButtons;
if (button != null)
{
if (e.NewValue != null)
{
SolidColorBrush brush = e.NewValue as SolidColorBrush;
button.myButton.Background = brush;
}
}
else
{
button.myButton.Background = null;
}
} public static readonly DependencyProperty FontColorProperty =
DependencyProperty.Register("FontColor", typeof(Brush), typeof(SilverlightButtons), new PropertyMetadata(FontColorChanged)); private static void FontColorChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
SilverlightButtons button = sender as SilverlightButtons;
if (button != null)
{
if (e.NewValue != null)
{
SolidColorBrush brush = e.NewValue as SolidColorBrush;
button.myText.Foreground = brush;
}
}
else
button.myText.Foreground = null;
} public static readonly DependencyProperty DefaultStrokeProperty =
DependencyProperty.Register("DefaultStroke", typeof(Brush), typeof(SilverlightButtons), new PropertyMetadata(DefaultStrokeChanged)); private static void DefaultStrokeChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
SilverlightButtons button = sender as SilverlightButtons;
if (button != null)
{
if (e.NewValue != null)
{
button.myRectangle.Stroke = e.NewValue as Brush;
}
}
else
{
button.myRectangle.Stroke = new SolidColorBrush(Colors.Transparent);
}
} public static readonly DependencyProperty HighLightStrokeProperty =
DependencyProperty.Register("HighLightStroke", typeof(Brush), typeof(SilverlightButtons), new PropertyMetadata(HighLightStrokeChanged)); private static void HighLightStrokeChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
SilverlightButtons button = sender as SilverlightButtons;
if (button != null)
{
if (e.NewValue != null)
{
button.myRectangle.Stroke = e.NewValue as Brush;
}
}
else
{
button.myRectangle.Stroke = null;
}
} public static readonly DependencyProperty DefaultFillProperty =
DependencyProperty.Register("DefaultFill", typeof(Brush), typeof(SilverlightButtons), new PropertyMetadata(DefaultFillChanged)); private static void DefaultFillChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
SilverlightButtons button = sender as SilverlightButtons;
if (button != null)
{
if (e.NewValue != null)
{
button.myRectangle.Fill = e.NewValue as Brush;
}
}
else
{
button.myRectangle.Fill = new SolidColorBrush(Colors.Transparent);
}
} public static readonly DependencyProperty HighLightFillProperty =
DependencyProperty.Register("HighLightFill", typeof(Brush), typeof(SilverlightButtons), new PropertyMetadata(HighLightFillChanged)); private static void HighLightFillChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
SilverlightButtons button = sender as SilverlightButtons;
if (button != null)
{
if (e.NewValue != null)
{
button.myRectangle.Fill = e.NewValue as Brush;
}
}
else
{
button.myRectangle.Fill = null;
}
} public static readonly DependencyProperty StrokeThicknessProperty =
DependencyProperty.Register("StrokeThickness", typeof(double), typeof(SilverlightButtons), new PropertyMetadata(StrokeThicknessChanged)); private static void StrokeThicknessChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
SilverlightButtons button = sender as SilverlightButtons;
if (button != null)
{
if (e.NewValue != null)
{
button.myRectangle.StrokeThickness = Convert.ToDouble(e.NewValue);
}
}
else
{
button.myRectangle.StrokeThickness = 1;
}
} public static readonly DependencyProperty RadiusXProperty =
DependencyProperty.Register("RadiusX", typeof(double), typeof(SilverlightButtons), new PropertyMetadata(RadiusXChanged)); private static void RadiusXChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
SilverlightButtons button = sender as SilverlightButtons;
if (button != null)
{
if (e.NewValue != null)
{
button.myRectangle.RadiusX = Convert.ToDouble(e.NewValue);
}
}
else
{
button.myRectangle.RadiusX = 0;
}
} public static readonly DependencyProperty RadiusYProperty =
DependencyProperty.Register("RadiusY", typeof(double), typeof(SilverlightButtons), new PropertyMetadata(RadiusYChanged)); private static void RadiusYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
SilverlightButtons button = sender as SilverlightButtons;
if (button != null)
{
if (e.NewValue != null)
{
button.myRectangle.RadiusY = Convert.ToDouble(e.NewValue);
}
}
else
{
button.myRectangle.RadiusY = 0;
}
} }
}
自定义控件做好后就可以运用该控件了,如下图,鼠标移上去会出现边框

xaml代码
<UserControl x:Class="SilverlightCreate.CustomControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:custom="clr-namespace:SilverlightCreate"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400"> <Grid x:Name="LayoutRoot" Background="#007F48">
<StackPanel Orientation="Horizontal" Height="30" HorizontalAlignment="Center">
<custom:SilverlightButtons x:Name="btnManyou" Contents="漫游" FontColor="White" IconSource="images/tool_manyou.png" ToolTipService.ToolTip="漫游" Margin="5" MouseMove="btnManyou_MouseMove" MouseLeave="btnManyou_MouseLeave" />
<custom:SilverlightButtons x:Name="btnDraw" Contents="重画" FontColor="White" IconSource="images/tool_chonghua.png" ToolTipService.ToolTip="重画" Margin="5" MouseMove="btnDraw_MouseMove" MouseLeave="btnDraw_MouseLeave" />
</StackPanel>
</Grid>
</UserControl>
cs代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes; namespace SilverlightCreate
{
public partial class CustomControl : UserControl
{
public CustomControl()
{
InitializeComponent();
} private void btnManyou_MouseMove(object sender, MouseEventArgs e)
{
ShowBorder(btnManyou);
} private void btnManyou_MouseLeave(object sender, MouseEventArgs e)
{
HideBorder(btnManyou);
} private void btnDraw_MouseMove(object sender, MouseEventArgs e)
{
ShowBorder(btnDraw);
} private void btnDraw_MouseLeave(object sender, MouseEventArgs e)
{
HideBorder(btnDraw);
} private void ShowBorder(SilverlightButtons button)
{
button.StrokeThickness = ;
button.HighLightStroke = new SolidColorBrush(Colors.White);
button.RadiusX = ;
button.RadiusY = ;
} private void HideBorder(SilverlightButtons button)
{
button.StrokeThickness = ;
}
}
}
参考文章
https://social.msdn.microsoft.com/Forums/silverlight/en-US/630cade8-349d-410e-9039-3a4b74c56ac9/silverlight-4-custom-control-binding?forum=silverlightarchieve
相关文章
http://www.cnblogs.com/yayx/archive/2008/06/03/1213126.html
http://developer.51cto.com/art/201003/191692.htm
Silverlight 用DependencyProperty 自定义ImageButton控件 定义属性的更多相关文章
- 在Attribute Inspector 上显示自定义的控件的属性
FirstColor 跟 CornerRadious 都是新增的显示属性具体实现方法如下: @property(nonatomic,weak)IBInspectable UIColor *firstC ...
- Android自定义组合控件详细示例 (附完整源码)
在我们平时的Android开发中,有时候原生的控件无法满足我们的需求,或者经常用到几个控件组合在一起来使用.这个时候,我们就可以根据自己的需求创建自定义的控件了,一般通过继承View或其子类来实现. ...
- 自定义PropertyGrid控件【转】
自定义PropertyGrid控件 这篇随笔其实是从别人博客上载录的.感觉很有价值,整理了一下放在了我自己的博客上,希望原作者不要介意. 可自定义PropertyGrid控件的属性.也可将属性名称显示 ...
- 用freemarker定义宏实现自定义公用控件
参考文章: Freemarker自定义标签的简单分析 定义一个基本的文本框:传入参数为:resourceName idName resourceVal="" idVal=" ...
- Silverlight自定义数据绑定控件应该如何处理IEditableObject和IEditableCollectionView对象
原文:Silverlight自定义数据绑定控件应该如何处理IEditableObject和IEditableCollectionView对象 原创文章,如需转载,请注明出处. 最近在一直研究Silve ...
- WPF 自定义Button控件及样式
这次通过最近做的小例子说明一下自定义Button控件和样式. 实现的效果为:
- Windows phone 自定义用户控件(UserControl)——ColorPicker
编码前 学习Windows phone自定义用户控件,在<WPF编程宝典>学习的小例子.并根据windows phone稍微的不同,做了点修改.ColorPicker(颜色拾取器):拥有三 ...
- 演练:使用属性自定义 DataGrid 控件
演练:使用属性自定义 DataGrid 控件 Silverlight 此主题尚未评级 - 评价此主题 Silverlight DataGrid 控件支持常用表格式设置选项,例如交替显示不同的行 ...
- WPF开发随笔收录-自定义图标控件
一.前言 1.在以前自学的过程中,软件需要使用到图标的时候,总是第一个想法是下载一个图片来充当图标使用,但实际得出来的效果会出现模糊的现象.后来网上学习了字体图标库的用法,可以在阿里云矢量图网站那里将 ...
随机推荐
- python运算优先级
运算符优先级(下面的优先级高) 运算符 描述 lambda Lambda表达式 or 布尔“或” and 布尔“与” not x 布尔“非” in not in 成员测试 is ...
- 天梯赛 L2-006 树的遍历(序列建树)
L2-006 树的遍历 (25 分) 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出一个正整数N(≤30),是二叉树中结点 ...
- 国内物联网平台(7):Ablecloud物联网自助开发和大数据云平台
国内物联网平台(7)——Ablecloud物联网自助开发和大数据云平台 马智 平台定位 面向IoT硬件厂商,提供设备联网与管理.远程查看控制.定制化云端功能开发.海量硬件数据存储与分析等基础设施,加速 ...
- chrome安装postman插件
参考http://www.cnplugins.com/zhuanti/how-to-make-crx-install.html 下载地址:http://www.cnplugins.com/down/p ...
- WebStorm设置Themes
1.首先去 http://www.phpstorm-themes.com/ 选择你喜欢的主题,保存对应主题的xml文件到你本地 2.打开C:\Users\Administrator\.WebStor ...
- Win10每次开机总是自动弹出MSN网址导航如何取消
Win10每次开机总是自动弹出MSN网址导航如何取消 近来有用户在升级Win10系统后,每次开机总是会自动弹出MSN中文网的网址导航.如果不想要开机打开MSN网址导航,那么应该怎么设置来取消呢?对此, ...
- 乱序优化与GCC的bug
以下内容来自搜狗实验室技术交流文档,搜狐公司研发中心版权所有,仅供技术交流 摘要 --------- 乱序优化是现代编译器非常重要的特性,本文介绍了什么是乱序优化,以及由此引发的一个bug,希 ...
- 区块链中的密码学(-)区块链中运用最广的散列算法-SHA256算法分析与实现
在很多技术人员的眼中,区块链并不是一种新的技术,而是过去很多年计算机技术的组合运用.而在这个方方面面技术的运用上,基于密码学的加密算法可以说是区块链各种特点得以表现的根本,一旦目前使用的加密算法被证实 ...
- 洛谷P3674 小清新人渣的本愿(莫队)
传送门 由乃tql…… 然后抄了一波zcy大佬的题解 我们考虑把询问给离线,用莫队做 然后用bitset维护,每一位代表每一个数字是否存在,记为$now1$ 然后再记录一个$now1$的反串$now2 ...
- CF1093E Intersection of Permutations 树状数组套权值线段树
\(\color{#0066ff}{ 题目描述 }\) 给定整数 \(n\) 和两个 \(1,\dots,n\) 的排列 \(a,b\). \(m\) 个操作,操作有两种: \(1\ l_a\ r_a ...