1.数据绑定
2.依赖项属性定义/注册
3.自定义模板(ControlTemplate)
SliderEx是一个简单的用户控件,由一个Slider和一个textblock组成,主要功能就是提供一个实时查看slider改变值的功能,通过该控件主要让大家掌握wpf中开发用户自定控件一些基本步骤
1. 定义一个资源文件SlideEx_Style.xaml 用于存放自定义控件的样式
2.SlideEx.cs文件,类SliderEx的实现
SlideEx_Style.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication1"> <!--文本框样式--> <Style x:Key="TextBoxStyle" TargetType="{x:Type TextBox}" > <Setter Property="Background" Value="Transparent"></Setter> <Setter Property="BorderBrush" Value="Transparent"></Setter> <Setter Property="BorderThickness" Value="0"></Setter> </Style> <Style TargetType="{x:Type local:SlideEx}"> <!-- 重写控件模板 , 忽略对其他属性的设置 --> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:SlideEx}"> <Border BorderBrush="Transparent" BorderThickness="0"> <StackPanel Orientation="Horizontal"> <Slider x:Name="PART_Slider" Height="30" HorizontalAlignment="Left" VerticalAlignment="Center" VerticalContentAlignment="Center" Maximum="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type local:SlideEx}}, Path=MaxValue}" Minimum="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type local:SlideEx}}, Path=MinValue}" Value="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type local:SlideEx}}, Path=Value}" Width="100" /> <TextBlock Width="40" Height="20" VerticalAlignment="Center" Margin="3,0,0,0" Text="{Binding ElementName=PART_Slider, Path=Value, Mode=OneWay}"/> </StackPanel> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary>
SlideEx.cs
public class SlideEx : Control{ static SlideEx() { //重写属性元数据 DefaultStyleKeyProperty.OverrideMetadata( typeof(SlideEx), new FrameworkPropertyMetadata(typeof(SlideEx))); MaxValueProperty = RegisterDP<double>("MaxValue", 100.0); MinValueProperty = RegisterDP<double>("MinValue", 0.0); ValueProperty = RegisterDP<int>("Value", 0); } /// <summary> /// 辅助方法,用于注册依赖项属性 /// </summary> /// <typeparam name="TProperty"></typeparam> /// <param name="propertyname"></param> /// <param name="defaultvalue"></param> /// <returns></returns> private static DependencyProperty RegisterDP<TProperty>(string propertyname , TProperty defaultvalue ){ return DependencyProperty.Register( propertyname, typeof( TProperty ), typeof( SlideEx), new PropertyMetadata( defaultvalue)); } ////如果要对属性添加回调操作可以这样处理 //DependencyProperty MaxValueProperty = DependencyProperty.Register( // "MaxValue", // typeof( double ), // typeof( SlideEx), // new PropertyMetadata( 100.0, OnMaxValueChanged)) // private static void OnMaxValueChanged(DependencyObject sender , DependencyPropertyChangedEventArgs e) // { // //当MaxValue值改变时候调用 // //这里可以爆出一些事件或者其他回调处理 // } //Slider的最大值 public static readonly DependencyProperty MaxValueProperty = null; //Slider的最小值 public static readonly DependencyProperty MinValueProperty = null; //slide的当前值 public static readonly DependencyProperty ValueProperty = null; //====================================================== // 依赖项属性的CLR属性包装 //===================================================== public double MaxValue { get { return (double)GetValue(MaxValueProperty); } set { SetValue(MaxValueProperty, value); } } public double MinValue { get { return (double)GetValue(MinValueProperty); } set { SetValue(MinValueProperty, value); } } public int Value { get { return (int)GetValue(ValueProperty); } set { SetValue(ValueProperty, value); } } //================================================ //如果想要获取用户控件内部的Slider和TextBlock控件 //可以在这里处理 //=============================================== private TextBlock _InternalTxtBlock = null; private Slider _internalSlider = null; public override void OnApplyTemplate() { base.OnApplyTemplate(); //通过GetTemplateChild获取内部控件(模板) //如果你的模板是基于DataTemplate的那么最好使用 //ViewTreeHelper的方法来实现,可以参考本blog //中另一篇博文ViewTreeHelper的扩展 _InternalTxtBlock = GetTemplateChild("PART_Text") as TextBlock; _internalSlider = GetTemplateChild("PART_Slider") as Slider; //to do something } }
MainWindow.cs中调用
<local:SlideEx Height="40"/>
该例子主要是为了演示在WPF项目开发中,开发一些自定义通过的用户控件,利用WPF特性可以构建出很多强壮友好的用户控件.