WPF,这个自定义的路由事件为什么不能冒泡
一个自定义的Person类,注册了依赖项属性和路由事件
class Person : UIElement
{
public static readonly DependencyProperty AgeProperty = DependencyProperty.Register("Age", typeof(int),
typeof(Person), new PropertyMetadata(25, new PropertyChangedCallback(OnAgeChanged),
new CoerceValueCallback(CoerceValue)));
public int Age
{
get { return (int)GetValue(AgeProperty); }
set { SetValue(AgeProperty, value); }
}
public static void OnAgeChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)
{
Person person = (Person)d;
RoutedPropertyChangedEventArgs<int> e = new RoutedPropertyChangedEventArgs<int>(
(int)args.OldValue, (int)args.NewValue, AgeChangedEvent);
person.OnName(e);
}
private static object CoerceValue(DependencyObject element, object value)
{
int newValue = (int)value;
newValue = Math.Max(1, Math.Min(100, newValue));
return newValue;
}
public static readonly RoutedEvent AgeChangedEvent = EventManager.RegisterRoutedEvent(
"AgeChanged", RoutingStrategy.Bubble,typeof(RoutedPropertyChangedEventHandler<int>), typeof(Person));
public event RoutedPropertyChangedEventHandler<int> AgeChanged
{
add { AddHandler(AgeChangedEvent, value); }
remove { RemoveHandler(AgeChangedEvent, value); }
}
protected virtual void OnName(RoutedPropertyChangedEventArgs<int> args)
{
RaiseEvent(args);
}
}
<Window x:Class="WPF2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPF2"
Title="MainWindow" Height="477" Width="958" local:Person.AgeChanged="Person_AgeChanged">
<Window.Resources>
<local:Person x:Key="person1" Age="25" AgeChanged="Person_AgeChanged"/>
</Window.Resources>
<Grid HorizontalAlignment="Left" Height="446" VerticalAlignment="Top" Width="950">
<Button Name="button1" Content="{Binding Source={StaticResource person1}, Path=Age}" FontSize="50" Margin="276,112,417,174" Click="Button_Click"/>
</Grid>
</Window>
AgeChanged事件是冒泡事件,在XAML中,我想在窗口定义Person的AgeChanged的附加事件,为什么行不通呢?该怎么写呢? --------------------编程问答-------------------- 此例参照自MSDN,
那个例子也不能使用附加属性啊 --------------------编程问答-------------------- 有人知道吗? --------------------编程问答-------------------- 有人知道吗?
补充:.NET技术 , C#