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

WPF-单击按钮时设置焦点-无代码共享

时间:2023-04-10 16:57:11 C#

WPF-单击按钮时设置焦点-无代码有没有办法使用WPF触发器将焦点从一个控件设置到另一个控件?如下例所示:有没有办法让这个EventTrigger专注于文本框“txtName”?我试图找到一种方法来使用严格的MVVM来做这样的事情。如果这是不应该通过XAML(在MVVM中)完成的事情,那也没关系。但我希望看到一些关于它如何适应XAML之外的MVVM模式的文档。您是否考虑过使用附加行为?它们易于实现和使用AttachedProperty。虽然它仍然需要代码,但该代码在类中被抽象出来并被重用。它们可以消除对“代码隐藏”的需要,并且经常与MVVM模式一起使用。试试这个,看看它是否适合你。公共类EventFocusAttachment{publicstaticControlGetElementToFocus(Buttonbutton){return(Control)button.GetValue(ElementToFocusProperty);}publicstaticvoidSetElementToFocus(Buttonbutton,Controlvalue){button.SetValue(ElementToFocusProperty,value);DependencyPropertyElementToFocusProperty=DependencyProperty.RegisterAttached("ElementToFocus",typeof(Control),typeof(EventFocusAttachment),newUIPropertyMetadata(null,ElementToFocusPropertyChanged));publicstaticvoidElementToFocusPropertyChanged(DependencyObjectsender,DependencyPropertyChangedEventArgse){varbutton=senderasButton;if(button!=null){button.Click+=(s,args)=>{Controlcontrol=GetElementToFocus(button);如果(控制!=null){control.Focus();}};然后在你的XAML中做类似的事情......我不在visualstudio附近所以我现在不能尝试这个,但是在我的头脑中你应该能够做这样的事情:FocusManager.FocusedElement="{BindingElementName=txtName}">编辑:这是一个后续问题(最近问):如何仅在xaml中设置自动对焦?其中包含此方法,以及如何使用它的一些不同想法您还可以使用WPF行为...publicclassFocusElementAfterClickBehavior:Behavior{protectedoverridevoidOnAttached(){_AssociatedButton=AssociatedObject;_AssociatedButton.Click+=AssociatedButton}protectedoverridevoidOnDetaching(){_AssociatedButton.Click-=AssociatedButtonClick;}voidAssociatedButtonClick(objectsender,RoutedEventArgse){Keyboard.Focus(FocusElement);}publicControlFocusElement{get{return(Control)EValue;ProFocus(set{SetValue(FocusElementProperty,value);}}//使用DependencyProperty作为FocusElement的后备存储。这会启用动画、样式、绑定等...publicstaticreadonlyDependencyPropertyFocusElementProperty=DependencyProperty.Register(",FocusElement"(Control),typeof(FocusElementAfterClickBehavior),newUIPropertyMetadata());}这是使用该行为的XAML。包括命名空间:xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"xmlns:local="clr-namespace:WpfApplication1"将WPF行为附加到按钮并绑定您想要的元素专注于To:所以这样你就没有代码,它可以重用于从ButtonBase继承的任何控件。希望这对某人有帮助。您需要一个TriggerAction来调用所需控件上的Focus()方法。publicclassSetFocusTrigger:TargetedTriggerAction{protectedoverridevoidInvoke(objectparameter){if(Target==null)返回;目标.焦点();}}要将焦点设置到Control,请放置一个Triggers集合,选择Event作为触发器并选择SetFocusTrigger作为要运行的类。在SetFocusTrigger声明中,您使用TargetName属性放置要接收焦点的控件的名称。这是你想要的吗?-->C#:publicvoidMoveFocusOnClick(objectsender,RoutedEventArgse){Keyboard.Focus(txtName);//或者你自己的逻辑}这与IanOakes的解决方案相同,但我做了一些小改动。按钮类型可以更通用,即ButtonBase,以处理更多情况,例如切换按钮。目标类型也可以更通用,即UIElement。从技术上讲,我认为这可能是一个IInputElement。我将事件处理程序设为静态,以便每次都不会生成运行时闭包。为了确保它保持静止,我将它移出了lambda。非常感谢伊恩。公共密封类EventFocusAttachment{[DebuggerStepThrough]publicstaticUIElementGetTarget(ButtonBaseb){return(UIElement)b.GetValue(TargetProperty);}[DebuggerStepThrough]publicstaticvoidSetTarget(ButtonBaseb,UIElementtarget){b.SetValue(Targettarget);}publicstaticreadonlyDependencyPropertyTargetProperty=DependencyProperty.RegisterAttached("Target",typeof(UIElement),typeof(EventFocusAttachment),newUIPropertyMetadata(null,TargetPropertyChanged));publicstaticvoidTargetPropertyChanged(DependencyObjecto,DependencyPropertyChangedEventArgs_){ButtonBasebb;if((bb=oasButtonBase)!=null)bb.Click+=handler;}staticvoidhandler(Objecto,RoutedEventArgs_){UIElement目标;如果((target=GetTarget((ButtonBase)o))!=null)target.Focus();}};用法与上面基本相同:注意事件可以定位/聚焦原始按钮本身。看看您是否正在使用任何Dispatcher那么它会有所帮助,但这是我在代码中使用的一个小技巧。只需在XAML中使用Loaded事件并创建一个新的处理程序。将此代码粘贴到该处理程序中!你准备好你的加载事件在这里有一些参数......PS:它需要Windows。如果你不懂线程;)以上就是C#学习教程:WPF-点击按钮时设置焦点-无代码分享全部内容,如果对大家有用需要进一步了解C#学习教程,希望大家多多关注~本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: