[英]How to override the onTouchUp in the user control which can be used to fire the event not only on this user control, but whole application
我是C#和WPF的新手。 現在,我想在我的用戶控件中覆蓋onTouchUp
事件。 然后我可以在引用中添加用戶控件以供重用。 問題是每次我想在應用程序上測試此事件時,事件僅在用戶控件區域觸發,而不是整個屏幕。 有人有解決方案嗎?
基本上,您需要在元素上附加到PreviewTouchUp事件,該元素覆蓋交互區域。 它也可能是應用程序窗口。 不建議在窗口外處理鼠標或觸摸事件。
這是一個示例,如何將任何行為直接附加到xaml中的任何元素:
<Window my:MyBehaviour.DoSomethingWhenTouched="true" x:Class="MyProject.MainWindow">
public static class MyBehaviour
{
public static readonly DependencyProperty DoSomethingWhenTouchedProperty = DependencyProperty.RegisterAttached("DoSomethingWhenTouched", typeof(bool), typeof(MyBehaviour),
new FrameworkPropertyMetadata( DoSomethingWhenTouched_PropertyChanged));
private static void DoSomethingWhenTouched_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var uiElement = (UIElement)d;
//unsibscribe firt to avoid multiple subscription
uiElement.PreviewTouchUp -=uiElement_PreviewTouchUp;
if ((bool)e.NewValue){
uiElement.PreviewTouchUp +=uiElement_PreviewTouchUp;
}
}
static void uiElement_PreviewTouchUp(object sender, System.Windows.Input.TouchEventArgs e)
{
//you logic goes here
}
//methods required by wpf conventions
public static bool GetDoSomethingWhenTouched(UIElement obj)
{
return (bool)obj.GetValue(DoSomethingWhenTouchedProperty);
}
public static void SetDoSomethingWhenTouched(UIElement obj, bool value)
{
obj.SetValue(DoSomethingWhenTouchedProperty, value);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.