当前位置: 首页 > 编程语言 > C#

如何在采集item模板时点击ListboxItem?分享

时间:2023-04-10 21:10:29 C#

点击item模板时如何捕获ListboxItem?我有一个带有ItemsTemplate集的ListBox。现在我想按Ctrl+左键单击ListBoxItem。我发现KeyBoard类应该给我修改键。现在我如何获得ListBoxItem上的点击事件?更好的是,我如何将其绑定到ICommand。我发现了一些点点滴滴,但无法弄清楚如何连接它们。看来InputBinding似乎对我或EventSetter有帮助。下面是使用ListBoxItem样式的EventSetter处理Ctrl+PreviewMouseLeftButtonDown的简单示例。这可能就是你想要的。XAML:Item1Item2Item3Item4代码隐藏:voidListBoxItem_PreviewMouseLeftButtonDown(objectsender,MouseButtonEventArgse){if((Keyboard.Modifiers&ModifierKeys.Control)>0){Console.WriteLine((senderasListBoxItem).Content.ToString());e.Handled=false;要将其绑定到ICommand,您可以使用其他行为,例如此处讨论的EventToCommand行为。编辑:为了解决您的评论,处理Click-event对于ListBoxItem来说有点棘手,因为有两件事:1)ListBoxItem没有点击事件,2)ListBoxItem在内部处理一些MouseEvents。不管怎样,我想出了一个模拟,附加的ClickEvent来让它工作。见下文。希望它有效。公共类AttachedEvents{privatestaticreadonlyDependencyPropertyIsTriggerEnabledProperty=DependencyProperty.RegisterAttached("IsTriggerEnabled",typeof(bool),typeof(FrameworkElement),newFrameworkPropertyMetadata(false));publicstaticreadonlyRoutedEventClickEvent;staticAttachedEvents(){try{ClickEvent=EventManager.RegisterRoutedEvent("点击",RoutingStrategy.Bubble,typeof(RoutedEventHandler),typeof(FrameworkElement));}catch(Exceptionex){}}privatestaticvoidSetIsTriggerEnabled(FrameworkElementelement,boolvalue){if(element!=null){element.SetValue(IsTriggerEnabledProperty,value);}}privatestaticboolGetIsTriggerEnabled(FrameworkElement元素){返回(元素!=null)?(bool)element.GetValue(IsTriggerEnabledProperty):false;}publicstaticvoidAddClickHandler(DependencyObjecto,RoutedEventHandlerhandler){FrameworkElementelement=(FrameworkElement)o;element.AddHandler(ClickEvent,手勒);element.MouseLeftButtonUp+=newSystem.Windows.Input.MouseButtonEventHandler(SimulatedClick_MouseLeftButtonUp);element.PreviewMouseLeftButtonDown+=newSystem.Windows.Input.MouseButtonEventHandler(SimulatedClick_MouseLeftButtonDown);}publicstaticvoidRemoveClickHandler(DependencyObjecto,RoutedEventHandlerhandler){FrameworkElementelement=(FrameworkElement)o;element.RemoveHandler(ClickEvent,处理器);element.MouseLeftButtonUp-=newSystem.Windows.Input.MouseButtonEventHandler(SimulatedClick_MouseLeftButtonUp);element.PreviewMouseLeftButtonDown-=newSystem.Windows.Input.MouseButtonEventHandler(SimulatedClick_MouseLeftButtonDown);}staticvoidSimulatedClick_MouseLeftButtonDown(objectsender,System.Windows.Input.MouseButtonEventArgse){FrameworkElementelement=(FrameworkElement)sender;UpdateIsTriggerSet(元素);鼠标捕获(元素);}staticvoidSimulatedClick_MouseLeftButtonUp(objectsender,System.Windows.Input.MouseButtonEventArgse){FrameworkElement元素=(FrameworkElement)sender;boolisTriggerSet=(bool)element.GetValue(IsTriggerEnabledProperty);//更新触发器集标志UpdateIsTriggerSet(element);//释放鼠标捕获Mouse.Capture(null);//如果设置了触发器并且我们仍在元素上方,那么我们将触发点击事件}}privatestaticboolIsMouseOver(FrameworkElementelement){Pointposition=Mouse.PrimaryDevice.GetPosition(element);if(((position.X>=0.0)&&(position.X=0.0)&&(position.Y=0.0)&&(position.X=0.0)&&(position.Y示例用法如下所示我似乎无法在XAML中设置附加事件(我不知道为什么)所以我必须在这里做一个解决方法。我所做的就是等待ListBoxItem加载并在代码隐藏附加事件处理程序中。XAML:...Code-behind:以上是C#学习教程:点击item模板时如何捕获ListboxItem?如果分享的内容对你有用,需要进一步了解C#学习教程,希望你多多关注---voidOnLoaded(objectsender,RoutedEventArgse){AttachedEvents.AddClickHandler((senderasListBoxItem),处理点击);}voidHandleClick(objectsender,RoutedEventArgse){if((Keyboard.Modifiers&ModifierKeys.Control)>0){Console.WriteLine("Ctrl+Clicked!");}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:

最新推荐
猜你喜欢