Silverlight 用DependencyProperty 为 自定义控 件定义属性

为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="24" d:DesignWidth="75">

    <Grid x:Name="LayoutRoot" Background="Transparent">
        <StackPanel x:Name="myButton" Orientation="Horizontal" >
            <Image x:Name="myImg"  />
            <TextBlock x:Name="myText" VerticalAlignment="Center" />
        </StackPanel>
    </Grid>

下面开始自定义属性内容,自定义属性要用 依赖属性类 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>
        /// 自定义button的文本
        /// </summary>
        public string Contents
        {
            get { return (string)GetValue(ContentsProperty); }
            set { SetValue(ContentsProperty, value); }
        }

        /// <summary>
        /// 自定义button的Icon
        /// </summary>
        public ImageSource IconSource
        {
            get { return (ImageSource)GetValue(IconSourceProperty); }
            set { SetValue(IconSourceProperty, value); }
        }

        /// <summary>
        /// 自定义button背景颜色
        /// </summary>
        public Brush ButtonBackGround
        {
            get { return (SolidColorBrush)GetValue(ButtonBackGroundProperty); }
            set { SetValue(ButtonBackGroundProperty, 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;
            }
        }

    }
}

自定义控件做好后就可以运用该控件了,效果如下图

xaml代码

<UserControl x:Class="SilverlightCreate.SilverlightButton"
    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="White">
        <custom:SilverlightButtons   Contents="test" IconSource="images/tool_chonghua.png" x:Name="silverlightButtons1" VerticalAlignment="Top" Height="27" Width="75"  />
    </Grid>
</UserControl>

参考文章

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

时间: 2024-08-02 11:02:20

Silverlight 用DependencyProperty 为 自定义控 件定义属性的相关文章

Silverlight中Image控件Stretch属性的四种值比较

通过设置Image控件Stretch属性的值可以控制图片的显示形式: 包含的值:None.Fill.Uniform.UniformToFill 1 <Grid x:Name="LayoutRoot" Background="White" Height="489" Width="603"> 2 <Image Height="150" HorizontalAlignment="Lef

C#控件事件属性大全

C#控件及常用设计整 1.窗体... 1 2.Label 控件... 3 3.TextBox 控件... 4 4.RichTextBox控件... 5 5.NumericUpDown 控件... 7 6.Button 控件... 7 7.GroupBox 控件... 7 8.RadioButton控件... 8 9.CheckBox 控件... 8 10.ListBox 控件... 9 11.ComboBox 控件... 10 12.CheckedListBox 控件... 10 13.Pict

Android中常用控件及属性

在之前的博客为大家带来了很多关于Android和jsp的介绍,本篇将为大家带来,关于Andriod中常用控件及属性的使用方法,目的方便大家遗忘时,及时复习参考.好了废话不多讲,现在开始我们本篇内容的介绍. 1.控制应用显示的方向: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖直显示效果. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LA

在C#中使用属性控件添加属性窗口

转自原文 在C#中使用属性控件添加属性窗口 第一步,创建在应用程序中将要展现的字段属性为public公有属性.其中,所有的属性必须有get和set的方法(如果不设置get方法,则要显示的属性不会显示在属性控件中).为了设置相关的属性,必须设置下面的一些关于属性控件的属性值,如下表所示: 属性值 含义 CategoryAttribute 该属性对在Property控件中的属性按字母顺序进行归类 DescriptionAttribute 其值为对每个属性的具体文字描述,将会显示在property控件

Kodak图像扫描控件的属性、事件、方法

Kodak图像扫描控件的属性.事件.方法 1. Kodak图像扫描控件的属性 (1)DestImageControl属性 字符型.该属性连接图像扫描控件到一个图像编辑控件,允许在扫描完毕后查看图像. (2)FileType属性 数值型.返回或设置图像扫描后建立的图像类型,属性值如表4-43所示. 表4-43                          FileType属性值 属性值 说明 1-Kodak Image Document (TIFF) TIFF文件 2-Fax Viewer D

服务器端控件的属性

服务器端控件的属性 分类: C#2013-03-13 08:12 621人阅读 评论(0) 收藏 举报 1)ClientID,控件在客户端的Id,控件在服务端的Id不一定等于客户端HTML中的Id,比如说在ListView等控件的模板中.因此如果要在客户端通过JavaScript Dom.JQuery的getElementById.$(“#id”)来操作控件的话最好不要直接写服务端Id,而是$(‘#<%=txt1.ClientID%>’).用JQuery事件设置鼠标移到控件上和从控件移开的不同

网络操作不能直接写在主线程中 以及 为什么不能在子线程中更新UI控件的属性

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //注意: 所有网络操作不能直接写在主线程中 因为所有的网络操作都是耗时的,如果加载到主线程中,会导致与用户的交互出现问题 ,所以要加载到子线程中 // [self loadImage]; [self performSelectorInBackground:@selector(loadImage) withObject:nil]; } //加

UI控件tag属性和魔法数字的处理

说明:tag属性有很大的用处,它就好像每个UI控件的id,当多个按钮指向同一个监听方法时,可以给方法带参数UIButton,然后根据不同的tag值 来判断执行哪个按钮的监听事件: - (IBAction)up:(UIButton *)sender // 该方法有四个btn指向,tag值是下边的,即可根据不同tag值执行相应的代码 { //upMoveTag的tag是10 //bottomMoveTag:11 //leftMoveTag:12 //rightMoveTag:13 CGRect re

android软件开发之TextView控件常用属性

TextView控件 text属性,设置显示的文本 textColor:设置文本颜色 textSize:设置文本字体大小 autoLink:设置文本为电话,URL连接等的时候是否显示为可点击的链接 cursorVisible:设定光标为显示或者隐藏,默认为显示 drawableTop:在文本上方输出一个drawable,如图片 drawableLeft.drawableBottom.drawableRight如上 drawablePadding:设置图片的外边距 singleLine:设置单行显