繁体   English   中英

如何以编程方式选择聚焦元素?

[英]How can I programmatically select the focused element?

有一个页面上有几个可聚焦元素(按钮,图像,...)在XAML中静态生成,一些动态地在代码后面生成。 在此页面上,按Tab键将逐个聚焦元素。 这意味着在当前元素周围显示虚线。 现在,我想要选出当前的焦点元素。 这意味着也要在它周围显示蓝线。 因此,当重点移动时,所选择的移动也是如此

我怎么能在C#代码隐藏中做到这一点?

在此输入图像描述

我想说最好的方法可能会有所不同,具体取决于你有哪些可聚焦元素? 如果你想为listboxitem做这个,你可以只用xaml这样做:

<UserControl.Resources>
    <Style TargetType="ListBoxItem">
        <Style.Triggers>
            <Trigger Property="IsFocused" Value="True">
                <Setter Property="Selector.IsSelected" Value="True"></Setter>
            </Trigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>
<ListBox>
    <ListBoxItem>
        Blah
    </ListBoxItem>
</ListBox>

类似的样式触发器也可以应用于其他可聚焦和可选择的元素。

编辑:

我认为你需要的是改变TabStop上的虚线。 该虚线表示选定的状态。 所以,它已被选中。 这个'选择聚焦元素'语句没有任何意义,因为如果您指的是TabStop,它确实已被选中。 您可以通过按Tab键然后按虚线来测试它,如果它是一个按钮并按Enter键,它将执行Click事件(如果它背后有一个处理程序)。

你需要的是这个

如果要在后面的代码中执行此操作,请将其添加到XAML的资源中。

<Style x:Key="MyFocusVisual">
      <Setter Property="Control.Template">
          <Setter.Value>
              <ControlTemplate>
                   <Rectangle Margin="-2" StrokeThickness="1" Stroke="Blue" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
</Style>
....
 myButton.FocusVisualStyle = (Style)FindResource("MyFocusVisual");

如果你没有XAML的访问权限,我想从中可以看出如何在代码隐藏中添加样式并将样式添加到按钮中。 现在,我显然在这里自相矛盾,因为你的目标是代码隐藏,不可能不能访问XAML吗? 通过XAML添加它比使用后面的代码更实用。

我会确保每个元素都是按钮或按钮的模板。 这样可以使用“IsTabStop”,或者可以聚焦元素。 使用MrsEd的解决方案,您可以使项目正确镶边:

<Button x:Class="Visualisation.UserControlButtonImage"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             IsTabStop="True">
    <Button.Template>
        <ControlTemplate>
           <Grid>
               <Image/> 
           </Grid>
        </ControlTemplate>
    </Button.Template>
</Button>

我在xml中做了什么。 我将PreviewGotKeyboardFocus添加到了我的窗口

             PreviewGotKeyboardFocus="FrmBase_OnPreviewGotKeyboardFocus"

在根元素(usercontrol或window)上使用PreviewGotKeyboardFocus来处理每个子控件上的焦点事件。

代码背后

private void FrmBase_OnPreviewGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
    try
    {
        dynamic item = e.NewFocus;
        item.IsSelected = true;
    }
    catch (Exception ex)
    {

    }
}

您引用的元素类型并非全部可选。

你想在C#中使用它,并且会动态添加一些元素,键盘将标记元素。 我建议跟踪你关注的项目,然后实现一个方法,根据当前关注的元素做你正在尝试做的事情。 使用键盘焦点方法。

protected override void OnPreviewGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
{
    var focusedElement = e.Source as IInputElement;
    // Do something here
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM