我有一个带有依赖项属性的用户控件。当该属性发生更改时,我想将RoutedEvent级联到主应用程序并执行某些功能。这是我的用户控制代码:
public TCardBase SelectedTCard
{
get { return (TCardBase)GetValue(SelectedTCardProperty); }
set { SetValue(SelectedTCardProperty, value); }
}
// Using a DependencyProperty as the backing store for SelectedTCard. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SelectedTCardProperty =
DependencyProperty.Register("SelectedTCard", typeof(TCardBase), typeof(TCardView), new PropertyMetadata(SelectedTCardPropertyChanged));
private static void SelectedTCardPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
//SEND EVENT HERE TO MAIN APPLICATION
}
将此活动发送到主应用程序的最佳方式是什么?感谢。
答案 0 :(得分:1)
网上有很多很好的例子如何创建自定义路由事件。
看看这段代码:
// This event uses the bubbling routing strategy
public static readonly RoutedEvent TapEvent = EventManager.RegisterRoutedEvent(
"Tap", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TCardView));
// Provide CLR accessors for the event
public event RoutedEventHandler Tap
{
add { AddHandler(TapEvent, value); }
remove { RemoveHandler(TapEvent, value); }
}
// This method raises the Tap event
void RaiseTapEvent()
{
RoutedEventArgs newEventArgs = new RoutedEventArgs(TCardView.TapEvent);
RaiseEvent(newEventArgs);
}
public TCardBase SelectedTCard
{
get { return (TCardBase)GetValue(SelectedTCardProperty); }
set { SetValue(SelectedTCardProperty, value); }
}
// Using a DependencyProperty as the backing store for SelectedTCard. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SelectedTCardProperty =
DependencyProperty.Register("SelectedTCard", typeof(TCardBase), typeof(TCardView), new PropertyMetadata(SelectedTCardPropertyChanged));
private static void SelectedTCardPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
((TCardView)o).RaiseTapEvent();
}
试一试。 :)