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> View Code下面開始自定義屬性內(nèi)容,自定義屬性要用 依賴屬性類 DependencyProperty
public static readonly DependencyProperty MyPropertyProperty =
??????????? DependencyProperty.Register("MyProperty", typeof(int), typeof(ownerclass), new UIPropertyMetadata(0));
DependencyProperty 的Register 方法中有四個參數(shù),第一個是自定的屬性,第二個自定義屬性的參數(shù)類型,第三個是自定義屬性所屬類,第四個是屬性元數(shù)據(jù)的實例,參數(shù)類型是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自定義控件做好后就可以運用該控件了,如下圖,鼠標(biāo)移上去會出現(xiàn)邊框
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 Codecs代碼
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
?
相關(guān)文章
http://www.cnblogs.com/yayx/archive/2008/06/03/1213126.html
http://developer.51cto.com/art/201003/191692.htm
?
轉(zhuǎn)載于:https://www.cnblogs.com/ZJ199012/p/4037053.html
總結(jié)
以上是生活随笔為你收集整理的Silverlight 用DependencyProperty 自定义ImageButton控件 定义属性的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【转载】从百度、360、搜狗对新站态度看
- 下一篇: Kali Linux GRUB修复