【问题标题】:Set focus to UIElement (i.e. TextBox ) in XAML , using Silverlight 4?使用 Silverlight 4 将焦点设置到 XAML 中的 UIElement (即 TextBox )?
【发布时间】:2011-02-08 17:26:42
【问题描述】:

我只看到了这个问题的代码解决方案。我正在寻找基于 XAML 的解决方案,但我很惊讶您无法将焦点集中在 XAML 中的 UI 元素上。

我发现了这个旧的 MSDN 帖子:http://social.msdn.microsoft.com/Forums/en/wpf/thread/09dc837d-4485-4966-b45b-266727bbb90c

有我寻求的解决方案(我猜这只是 WPF)

<Grid FocusManager.FocusedElement="{Binding ElementName=listBox1}">

Silverlight 4 XAML 中无法将焦点设置到 TextBox/ListBox 吗?

【问题讨论】:

    标签: xaml silverlight-4.0 uielement


    【解决方案1】:

    如果您足够努力,XAML 中总有办法。 :) 您需要的是来自 Blend SDK 的 Trigger

    public class FocusTrigger : TargetedTriggerAction<Control>
    {
       protected override void Invoke(object parameter)
       {
          if (Target == null)
             return;
    
          Target.Focus();
       }
    }
    

    然后像这样使用它:

    <Button Content="Move focus">
      <i:Interaction.Triggers>
        <i:EventTrigger EventName="Click">
          <local:FocusTrigger TargetName="TheTextBox"/>
        </i:EventTrigger>
      </i:Interaction.Triggers>
    </Button>
    <TextBox x:Name="TheTextBox"/>
    

    如果你想变得非常花哨,你可以apply a condition to your trigger 并在 XAML 中做各种疯狂的事情。我会说我很惊讶这种东西不是内置的。

    【讨论】:

      【解决方案2】:

      据我所知,不,XAML 中没有设置元素焦点的方法。你必须诉诸于你所引用的东西。我认为附加行为(类似于FocusManager)将是最佳途径。

      【讨论】:

      • 感谢您的确认,我还没有找到在 XAML 中执行此操作的方法,所以我不得不求助于其他方法。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多