-
(UserControl) DependencyProperty & RoutedEvent 만들기dev/wpf 2008. 8. 14. 05:28
UserControl에 DependencyProperty 만들기
MyControl이라는 UserControl에 double형의 값을 갖는MyValue라는 Property를 만들고,
MyValue의 값이 변경되면 event를 발생시키려고 할 때
C# Code UserControl MyControl Class
public static readonly DependencyProperty MyValueProperty =
DependencyProperty.Register("MyValue",
typeof(double), typeof(MyControl),
new FrameworkPropertyMetadata(double.NaN,
new PropertyChangedCallback(OnMyValueChanged),
new CoerceValueCallback(CoerceMyValue)));
static object CoerceMyValue(DependencyObject sender, object value)
{
return (double)value;
}
static void OnMyValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
MyControl es = sender as MyControl;
RoutedPropertyChangedEventArgs<double> changedEvent =
new RoutedPropertyChangedEventArgs<double>(
(double)e.OldValue, (double)e.NewValue, MyValueChangedEvent);
es.OnMyValueChanged(changedEvent);
}
public static readonly RoutedEvent MyValueChangedEvent = EventManager.RegisterRoutedEvent(
"MyValueChanged", RoutingStrategy.Bubble,
typeof(RoutedPropertyChangedEventHandler<double>), typeof(MyControl));
public event RoutedPropertyChangedEventHandler<double> MyValueChanged
{
add { AddHandler(MyValueChangedEvent, value); }
remove { RemoveHandler(MyValueChangedEvent, value); }
}
protected virtual void nMyValueChanged(RoutedPropertyChangedEventArgs<double> e)
{
RaiseEvent(e);
}