WPF 为控件添加依赖属性和事件RoutedEvent
一、创建UserControl项目
在VS中右键单击你的项目,点击"添加新项目",在出现的选择列表中选择"UserControl",VS会自动为你生成一个*.xaml文件以及其对应的后台代码文件(.cs或其它).
值得注意的是,自动生成的代码中,你的控件是继承于System.Windows.Controls.UserControl类的,这对应你的控件而言并不一定是最恰当的基类,你可以修改它,但注意你应该同时修改.cs文件和*.xaml文件中的基类,而不只是修改*.cs文件,否则当生成项目时会报错"不是继承于同一基类".修改*.xaml文件的方法是:将该文件的第一行和最后一行的"UserControl"改成与你认为恰当的基类名称.
二、创建依赖属性(依赖属性,DependencyProperty)
快捷操作 propdp
1.定义公开的静态字段
public static readonly DependencyProperty TimeProperty;
2.注册静态属性
在静态构造函数中,为静态依赖属性注册名称,类型,依赖对象和指定回调函数
static ClockForUserCtrl()
{
TimeProperty = DependencyProperty.Register("Time", typeof(DateTime), typeof(ClockForUserCtrl), new PropertyMetadata(new PropertyChangedCallback(TimePropertyChangedCallback)));
}
3.该依赖属性包装成普通属性
注意:在将依赖属性包装成普通属性时,在get和set块中除了按部就班的调用GetValue和SetValue方法外,不要进行任何其它的操作. [^1]
[Description("获取或设置当前日期和时间")]
[Category("Common Properties")]
public DateTime Time
{
get { return (DateTime)GetValue(TimeProperty); }
set { SetValue(TimeProperty, value); }
}
4.依赖属性回调函数实现
private static void TimePropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d != null && d is ClockForUserCtrl)
{
ClockForUserCtrl clock = d as ClockForUserCtrl;
// do something...
// clock.OnTimeUpdated((DateTime)e.OldValue, (DateTime)e.NewValue);
}
}
5.编写惯用的OnXXX方法:
protected virtual void OnTimeUpdated(DateTime oldValue, DateTime newValue)
{
RoutedPropertyChangedEventArgs<DateTime> arg =
new RoutedPropertyChangedEventArgs<DateTime>(oldValue, newValue, TimeUpdatedEvent);
this.RaiseEvent(arg);
}
三、为控件添加事件(传阅事件,RoutedEvent)
添加传阅事件的方法与添加依赖属性的方法很类似:
public static readonly RoutedEvent TimeUpdatedEvent =
EventManager.RegisterRoutedEvent(“TimeUpdated”,
RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventHandler), typeof(ClockUserCtrl));
其支持方法EventManager.RegisterRoutedEvent()对应的几个参数分别为:事件名称,事件传阅的方式(向上传阅,向下传阅或不传阅),事件对应的EventHandler的类型,事件拥有者的类型)
然后将事件包装成普通的.NET事件:
[Description(“日期或时间被更新后发生”)]
public event RoutedPropertyChangedEventHandler TimeUpdated
{
add
{
this.AddHandler(TimeUpdatedEvent, value);
}
remove
{
this.RemoveHandler(TimeUpdatedEvent, value);
}
}
注意,与依赖属性一样,不要在add与remove块中添加除AddHandler与RemoveHandler以外的代码.
题外话,事件参数中的e.Handled=true并不是终止事件的传阅,这只是为事件做一个标记而已,以便在默认情况下的让那些事件处理函数在该标记为true的情况下不被调用,要为该标记为true的事件注册处理方法并让该方法得到执行,请使用AddHandler方法,并把最后一个参数handlerEventsToo设置为true,如下:
this.myInkCanvas.AddHandler(
InkCanvas.MouseLeftButtonDownEvent,
new MouseButtonEventHandler(
myInkCanvas_MouseLeftButtonDown),
true);
private void myInkCanvas_MouseLeftButtonDown(
object sender, MouseButtonEventArgs e)
{
//do something
}
四、3,为控件添加命令(Commands)
首先我们定义一个命令:
public static readonly RoutedUICommand SpeakCommand = new RoutedUICommand("Speak", "Speak", typeof(ClockUserCtrl));
在静态构造函数中,注册
//参数分别为命名的显示名称,命令的名称,命令的拥有者类型.,
//控件的静态函数中定义一个命令绑定,该命令绑定定义了命令的具体细节:对应的命令是什么?其完成什么样的功能
CommandBinding commandBinding =new CommandBinding(SpeakCommand, new ExecutedRoutedEventHandler(ExecuteSpeak), new CanExecuteRoutedEventHandler(CanExecuteSpeak));
CommandManager.RegisterClassCommandBinding(typeof(ClockUserCtrl), commandBinding);
InputBinding inputBinding = new InputBinding(SpeakCommand, new MouseGesture(MouseAction.LeftClick));//指定快捷键
CommandManager.RegisterClassInputBinding(typeof(ClockUserCtrl), inputBinding);
private static void ExecuteSpeak(object sender, ExecutedRoutedEventArgs arg)
{
ClockUserCtrl clock = sender as ClockUserCtrl;
if (clock != null)
{
clock.SpeakTheTime();
}
}
private static void CanExecuteSpeak(object sender, CanExecuteRoutedEventArgs arg)
{
ClockUserCtrl clock = sender as ClockUserCtrl;
arg.CanExecute = (clock != null);
}
CanExecuteRoutedEventArgs的CanExecute属性用于指示当前命令是否可用,也就是说系统会不断地检视该命令与该命令的作用对象,并根据你所提供的条件来判断当前命令是否可用,比如文本框状态变为"只读"后,其"粘贴"命令将不可用,作用于该文本框的粘贴按钮会自动被禁用,反之则启用.
new ExecutedRoutedEventHandler(ExecuteSpeak)委托指定了当该命令被执行时所要完成的任务,这通过回调ExcuteSpeak函数来实现.
private static void ExecuteSpeak(object sender, ExecutedRoutedEventArgs arg)
{
ClockUserCtrl clock = sender as ClockUserCtrl;
if (clock != null)
{
clock.SpeakTheTime();
}
}
private void SpeakTheTime()
{
DateTime localTime = this.Time.ToLocalTime();
string textToSpeak = “现在时刻,” +
localTime.ToShortDateString() +","+
localTime.ToShortTimeString() +
“,星期” + (int)localTime.DayOfWeek;
this.speecher.SpeakAsync(textToSpeak);
}
我们也可以为命令添加快捷键,这是通过InputBinding来实现的,其将命令与命令的快捷键关联起来,比如:
InputBinding inputBinding = new InputBinding(SpeakCommand, new MouseGesture(MouseAction.LeftClick));
CommandManager.RegisterClassInputBinding(typeof(ClockUserCtrl), inputBinding);
这样,当我们鼠标点击控件时就会引发控件的Speak命令,从而调用SpeakTheTime函数进行语音播报.
快捷键可以通过MouseGesture或KeyGesture来定义.
4,优点与缺点:
正如在在WPF中自定义控件(1) 中谈到的一样,UserControl能比较快速的打造自定义控件,但其对模板样式等缺乏很好的支持,打造出来的控件不如WPF内置控件一样灵活,在本系列随笔的下一篇中,我们将介绍如何打造能对WPF新特性提供完全支持的CustomControl.
五、完整代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Windows.Threading;
using System.Speech.Synthesis;
namespace MyCtrlLib
{
/// <summary>
/// Interaction logic for ClockUserCtrl.xaml
/// </summary>
public partial class ClockUserCtrl : System.Windows.Controls.UserControl
{
public ClockUserCtrl()
{
InitializeComponent();
}
static ClockUserCtrl()
{
SetCommands();
}
private static void SetCommands()
{
CommandBinding commandBinding =new CommandBinding(SpeakCommand, new ExecutedRoutedEventHandler(ExecuteSpeak), new CanExecuteRoutedEventHandler(CanExecuteSpeak));
CommandManager.RegisterClassCommandBinding(typeof(ClockUserCtrl), commandBinding);
InputBinding inputBinding = new InputBinding(SpeakCommand, new MouseGesture(MouseAction.LeftClick));//指定快捷键
CommandManager.RegisterClassInputBinding(typeof(ClockUserCtrl), inputBinding);
}
private DispatcherTimer innerTimer;
private SpeechSynthesizer speecher = new SpeechSynthesizer();
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
this.SetTimer();
this.SetBinding();
this.SpeakTheTime();
}
private static void ExecuteSpeak(object sender, ExecutedRoutedEventArgs arg)
{
ClockUserCtrl clock = sender as ClockUserCtrl;
if (clock != null)
{
clock.SpeakTheTime();
}
}
private static void CanExecuteSpeak(object sender, CanExecuteRoutedEventArgs arg)
{
ClockUserCtrl clock = sender as ClockUserCtrl;
arg.CanExecute = (clock != null);
}
private void SpeakTheTime()
{
DateTime localTime = this.Time.ToLocalTime();
string textToSpeak = "现在时刻," +
localTime.ToShortDateString() +","+
localTime.ToShortTimeString() +
",星期" + (int)localTime.DayOfWeek;
this.speecher.SpeakAsync(textToSpeak);
}
private void SetTimer()
{
this.innerTimer = new DispatcherTimer(TimeSpan.FromSeconds(1.0),
DispatcherPriority.Loaded, new EventHandler(this.InnerTimerCallback), this.Dispatcher);
this.innerTimer.Start();
}
private void InnerTimerCallback(object obj, EventArgs arg)
{
this.Time = DateTime.Now;
}
private void SetBinding()
{
Binding timeBinding = new Binding();
timeBinding.Source = this;
timeBinding.Path = new PropertyPath(ClockUserCtrl.TimeProperty);
timeBinding.Converter = new TimeConverter();
this.textBlock_Time.SetBinding(TextBlock.TextProperty, timeBinding);
}
#region DP
public static readonly DependencyProperty TimeProperty =
DependencyProperty.Register("Time", typeof(DateTime), typeof(ClockUserCtrl),
new FrameworkPropertyMetadata(DateTime.Now,new PropertyChangedCallback(TimePropertyChangedCallback)));
[Description("获取或设置当前日期和时间")]
[Category("Common Properties")]
public DateTime Time
{
get
{
return (DateTime)this.GetValue(TimeProperty);
}
set
{
this.SetValue(TimeProperty, value);
}
}
private static void TimePropertyChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs arg)
{
if (sender != null && sender is ClockUserCtrl)
{
ClockUserCtrl clock = sender as ClockUserCtrl;
clock.OnTimeUpdated((DateTime)arg.OldValue, (DateTime)arg.NewValue);
}
}
#endregion
#region Event
public static readonly RoutedEvent TimeUpdatedEvent =
EventManager.RegisterRoutedEvent("TimeUpdated",
RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventHandler<DateTime>), typeof(ClockUserCtrl));
[Description("日期或时间被更新后发生")]
public event RoutedPropertyChangedEventHandler<DateTime> TimeUpdated
{
add
{
this.AddHandler(TimeUpdatedEvent, value);
}
remove
{
this.RemoveHandler(TimeUpdatedEvent, value);
}
}
protected virtual void OnTimeUpdated(DateTime oldValue, DateTime newValue)
{
RoutedPropertyChangedEventArgs<DateTime> arg =
new RoutedPropertyChangedEventArgs<DateTime>(oldValue, newValue,TimeUpdatedEvent);
this.RaiseEvent(arg);
}
#endregion
#region Commands
public static readonly RoutedUICommand SpeakCommand = new RoutedUICommand("Speak", "Speak", typeof(ClockUserCtrl));
#endregion
}
#region Converter
[ValueConversion(typeof(DateTime),typeof(string))]
public class TimeConverter : IValueConverter
{
#region IValueConverter 成员
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
DateTime date = (DateTime)value;
return date.ToLocalTime().ToLongTimeString();
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
#endregion
}
[ValueConversion(typeof(DateTime),typeof(string))]
public class DateConverter : IValueConverter
{
#region IValueConverter 成员
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
DateTime date = ((DateTime)value).ToLocalTime();
return date.ToShortDateString() + " " + date.DayOfWeek;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
#endregion
}
#endregion
}