为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>

View Code

下面开始自定义属性内容,自定义属性要用 依赖属性类 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;}}elsebutton.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;}}}
}

View Code

自定义控件做好后就可以运用该控件了,如下图,鼠标移上去会出现边框

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>

View Code

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 = 1;button.HighLightStroke = new SolidColorBrush(Colors.White);button.RadiusX = 10;button.RadiusY = 10;}private void HideBorder(SilverlightButtons button){button.StrokeThickness = 0;}}
}

View Code

参考文章

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

转载于:https://www.cnblogs.com/ZJ199012/p/4037053.html

Silverlight 用DependencyProperty 自定义ImageButton控件 定义属性相关推荐

  1. 自定义用户控件显示属性分类、描述、默认值

    在网上查了相关资料,大概例子如下: using System.ComponentModel; bool _IsCheck = true;     [Bindable(true), DefaultVal ...

  2. [转] 使用模板自定义 WPF 控件

      [转] 使用模板自定义 WPF 控件                                                                                 ...

  3. 自定义用户控件的使用

    使用VS C#无论是WinForm开发,还是WebForm 开发都会遇到一个问题:在Form布局时,会重复使用相同的一组控件.如下图中的控件 这一组控件是非常常用的,但是如果每一次布局相似的窗体时,都 ...

  4. Silverlight AutoCompleteBox(自动完成输入框控件)使用方法

         在微软的Silverlight 开源控件项目"Silverlight Toolkit"中,提供了一个自动完成输入框控件:AutoCompleteBox.          ...

  5. WPF自定义仪表盘控件

    WPF自定义仪表盘控件 一.前言 二.功能实现 一.前言 在学习和工作中使用WPF时,都离不开自定义控件的使用,今天分享一个自己在学习过程中使用到的一个自定义仪表盘控件,感觉挺不错的,在这里分享给大家 ...

  6. WPF 自定义TabControl控件样式(转)

    WPF 自定义TabControl控件样式 一.前言 程序中经常会用到TabControl控件,默认的控件样式很普通.而且样式或功能不一定符合我们的要求.比如:我们需要TabControl的标题能够居 ...

  7. WPF控件之自定义TextBox控件

    首先我们要知道用户控件与自定义用户控件的确保 用户控件 1将多个现有的控件组合成一个可重用的"组". 2不能使用样式和模板. 3继承自UserControl类. 自定义控件 1在现 ...

  8. WPF 滚动条控件ScrollViewer的使用及自定义滚动条控件(一)

    WPF 滚动条控件ScrollViewer的使用及自定义滚动条控件(一) 首先看一下两种空间的运行效果: 左边是自定义滑条控件,右边是自带的滑条控件: **滑条使用方法:**我们在ScrollView ...

  9. Android View体系(十)自定义组合控件

    相关文章 Android View体系(一)视图坐标系 Android View体系(二)实现View滑动的六种方法 Android View体系(三)属性动画 Android View体系(四)从源 ...

最新文章

  1. PowerDesigner生成数据库建表sql脚本
  2. linux的11186端口,linux – CentOS – semanage – 删除端口范围
  3. 前端开发面试题总结之——JAVASCRIPT.One
  4. rsync本地及远程复制备份【原创】
  5. php.exe系统错误,PhpStorm中报 “Cannot run program git.exe, 系统找不到指定的文件” 错误的解决方法...
  6. jmeter下TPS插件的安装
  7. 中英文字体对照 ueditor添加字体
  8. 2019新零售商家和企业,如何构建私域流量的思维和方法分享[附案例]
  9. xshell上传文件到虚拟机Linux服务器
  10. 含有一个量词的命题的否命题_高一 | 数学必修一全称量词与存在量词知识点总结...
  11. 编程入门指南 v1.5
  12. 一个程序员失败的爱情
  13. NoSQLBooster操作Mongodb的基本使用。
  14. 鱼香肉丝里到底有没有鱼?
  15. 大学英语综合教程三 Unit 8 课文内容英译中 中英翻译
  16. 庄懂的TA笔记(十三)<特效-混合模式:四种主要透明通道用法 AC,AB,AD,自定义混合>
  17. [国嵌攻略][054][NandFlash驱动设计_写]
  18. C++ 检查内存泄露工具
  19. 计算机专业cpa有前途吗,计算机专业CPA一年通过六科,学霸陈玺备考经验!
  20. 变年轻特效是什么软件?快把这些软件收好

热门文章

  1. JavaScript 正则表达式
  2. 前端开发工程师——网易云课堂
  3. 多生产者多消费者问题
  4. SAS编程基础 - 逻辑库和数据集
  5. 给source insight添加.cc的C++文件后缀识别(转载)
  6. Notepad++加上xml格式化的功能
  7. 最小生成树之Kruskal
  8. linux 下共享库创建及使用
  9. ZOJ 2588 Burning Bridges 割边
  10. 图解WordPress主题(模板)架构