silverlight在XAML中设置控件焦点

来源:互联网 发布:linux cp 排除文件夹 编辑:程序博客网 时间:2024/04/30 13:01

参考:http://stackoverflow.com/questions/4936204/set-focus-to-uielement-i-e-textbox-in-xaml-using-silverlight-4
1.首先需要添加对System.Windows.Interactivity.dll程序集的引用,这是一个blend提供的程序集
2.定义一个类,继承自TargetedTriggerAction,如下
namespace my.Utils
{
    public class FocusTrigger : TargetedTriggerAction<Control>
    {
        protected override void Invoke(object parameter)
        {
            if (Target == null)
                return;

            Target.Focus();
        }
    }
}
3.在XAML如下:
<TextBox Height="63" Text="liancs" Name="textBox1" Width="279" />
        <Button Content="Button"  HorizontalAlignment="Left" Margin="110,26,0,0" VerticalAlignment="Top" Width="75">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Click">
                    <local:FocusTrigger TargetName="sv"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Button>
需要在XAML页面引入如下命称空间:
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:local="clr-namespace:my.Utils",my.Utils是FocusTrigger 类所在的命称空间。

原创粉丝点击