【问题标题】:focus visual not showing when navigating focus programically以编程方式导航焦点时焦点视觉未显示
【发布时间】:2013-01-12 00:07:27
【问题描述】:

每当我尝试以编程方式移动焦点时,焦点视觉对象(虚线矩形)都不会显示。

如何强制显示此视觉对象?

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Loaded="OnLoaded">
    <StackPanel>
        <TextBlock x:Name="a" Focusable="True">A</TextBlock>
        <TextBlock Focusable="True">B</TextBlock>
        <Button Focusable="False" Click="OnClick">Move Focus</Button>
    </StackPanel>
</Window>

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void OnLoaded(object sender, RoutedEventArgs e)
    {
        Keyboard.Focus(a);
    }

    private void OnClick(object sender, RoutedEventArgs e)
    {
        var request = new TraversalRequest(FocusNavigationDirection.Next);
        var elementWithFocus = Keyboard.FocusedElement as UIElement;
        if (elementWithFocus != null)
            elementWithFocus.MoveFocus(request);
    }
}

【问题讨论】:

    标签: wpf wpf-controls


    【解决方案1】:

    如果您(在反射器/ilspy 中)查看 KeyboardNavigation 的 ShowFocusVisual,您会发现框架仅在最后一次输入来自键盘时才会显示它(或者如果基于 KeyboardCues 系统参数信息的内部静态属性是真的)。所以我认为除了使用反射来临时设置该属性或异步聚焦元素并强制键盘操作(可能使用 winforms SendKeys 或 keybd_event api)之外,没有什么好的方法,但我也不推荐.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-21
      • 2011-12-18
      • 1970-01-01
      • 2015-03-12
      • 1970-01-01
      • 2021-10-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多