[英]Bind Events to UserControl in WPF
我如何从myWindow内的myUserControl传播PreviewKeyDown事件。
在myWindow.xaml中
<local:MyFilter x:Name="check" MyEvent="submit" />
在myUserContorl.xaml中
<ComboBox x:Name="combo" PreviewKeyDown="{Binding Path=MyEvent}" />
在myUserContorl.xaml.cs中
#region MyEvent
/// <summary>
/// Gets or sets the Label which is displayed next to the field
/// </summary>
public EventHandler MyEvent
{
get { return (EventHandler)GetValue(EventHandlerProperty); }
set { SetValue(EventHandlerProperty, value); }
}
/// <summary>
/// Identified the Label dependency property
/// </summary>
public static readonly DependencyProperty EventHandlerProperty =
DependencyProperty.Register("MyEvent", typeof(EventHandler),
typeof(myUserControl), new PropertyMetadata(""));
#endregion
这仅适用于“字符串字段”(如内容或文本),但不适用于事件
只需将事件处理程序添加(并删除)到基础控件中:
public event KeyEventHandler MyEvent
{
add { combo.AddHandler(PreviewKeyDownEvent, value); }
remove { combo.RemoveHandler(PreviewKeyDownEvent, value); }
}
在XAML中没有任何绑定:
<ComboBox x:Name="combo" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.