大佬教程收集整理的这篇文章主要介绍了Silverlight - 回车键触发默认按钮,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@L_696_0@常见的需求是,用户在某个TextBox中输入回车键,自动触发界面上的某个按钮。比如登录窗口中,用户输入完密码后按回车,自动触发登录按钮。@H_403_7@
@H_403_7@
@H_403_7@@H_403_7@@H_403_7@
Silverlight (4.0)目前还没有像winform程序那样可通过设置@L_696_0@属性值来简单实现。@H_403_7@
@H_403_7@
Patrick Cauldweel 提供了@L_696_0@方案, Kyle Burns 做了些改进,@L_674_20@了通用性,并解决了前者的一些缺点。@H_403_7@
@H_403_7@
受Kyle启发,我的实现方法是实现为@L_696_0@Panel (Grid,Canvas,StackPanel的基类)控件的Behavior。该实现需要引用System.Windows.Interactivity.dll@H_403_7@
@H_403_7@
@L_696_0@示例登录窗口的Xaml如下:@H_403_7@
<StackPanel>@H_403_7@
<i:Interaction.behaviors>@H_403_7@
<command:DefaultButtonBehavior DefaultButtonName="{Binding ELEMENTNAME=btnLogin, Path=Name}" />@H_403_7@
</i:Interaction.behaviors>@H_403_7@
<TextBox Text="{Binding LoginNamE}"/>@H_403_7@
<passwordBox password="{Binding LoginpassworD}"/>@H_403_7@
<Button Content="Login" x:Name="btnLogin" Command="{Binding LoginCommanD}"/>@H_403_7@
</StackPanel>@H_403_7@
@H_403_7@
@H_403_7@
DefaultButtonBehavior类(vb.net):@H_403_7@
@H_403_7@
Imports System.Windows.Interactivity Imports System.Windows.Data Imports System.Windows.Automation.Peers Imports System.Windows.Automation.Provider Imports System.Linq Namespace Commands Public Class DefaultButtonBehavior Inherits Behavior(Of Panel) Public Property DefaultButtonName As String Get Return CType(GetValue(DefaultButtonNameProperty), String) End Get Set(ByVal value As String) SETVALue(DefaultButtonNameProperty, value) End Set End Property Public Shared ReadOnly DefaultButtonNameProperty As DependencyProperty _ = DependencyProperty.Register(name:="DefaultButtonName", _ propertyType:=GetType(String), _ ownerType:=GetType(DefaultButtonBehavior), _ typeMetadata:=Nothing) Protected Overrides Sub OnAttached() @H_988_37@myBase.onAttached() AddHandler AssociatedObject.KeyUp, AddressOf AssociatedObject_KeyUp End Sub Protected Overrides Sub OnDetaching() @H_988_37@myBase.onDetaching() RemoveHandler AssociatedObject.KeyUp, AddressOf AssociatedObject_KeyUp End Sub Private Sub AssociatedObject_KeyUp(ByVal sender As Object, ByVal e As KeyEventArgs) If e.Key = Key.Enter Then Dim defaultButton As Button = AssociatedObject.Children.ofType(Of Button).FirstOrDefault(Function(X) x.Name = DefaultButtonName) If defaultButton Isnot Nothing And defaultButton.IsEnabled Then 'update binding source of textBox Dim inputTxts = AssociatedObject.Children.ofType(Of TextBox)() For Each txt In inputTxts Dim exp As BindingExpression = txt.GetBindingExpression(TextBox.TextProperty) exp.updatesource() Next 'update binding source of password Dim inputPwds = AssociatedObject.Children.ofType(Of passwordBox)() For Each pwd In inputPwds Dim exp As BindingExpression = pwd.GetBindingExpression(passwordBox.passwordProperty) exp.updatesource() Next Dim peer As New ButtonAutomationPeer(defaultButton) Dim invoke As IInvokeProvider = peer.GetPattern(PatternInterface.InvokE) invoke.Invoke() e.Handled = True End If End If End Sub End Class End Namespace
以上是大佬教程为你收集整理的Silverlight - 回车键触发默认按钮全部内容,希望文章能够帮你解决Silverlight - 回车键触发默认按钮所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。