简体   繁体   English

如何以编程方式显示FocusVisualStyle?

[英]How to show programmatically FocusVisualStyle?

Afters attemps I could tell that the FocusVisualStyle is only activated by the keyboard (tab and arrows keys). 尝试之后,我可以说FocusVisualStyle仅由键盘(选项卡和箭头键)激活。

Try to make the FocusVisualStyle to be applied after the component is loaded, it is impossible to do, There is an easy way to get around this problem? 尝试使FocusVisualStyle在组件加载后才能应用,这是不可能的,有一种简单的方法可以解决此问题?

I found this: 我找到了这个:
- focus visual not showing when navigating focus programically -以编程方式导航焦点时未显示焦点视觉
- How do WPF buttons decide to show FocusVisualStyle? -WPF按钮如何决定显示FocusVisualStyle?
- http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/99856840-f8ef-4547-9150-c4c46ec2f3df -http://social.msdn.microsoft.com/Forums/zh-CN/wpf/thread/99856840-f8ef-4547-9150-c4c46ec2f3df

But none shows a definite solution (without overwriting the component), and I could not write, can someone help? 但是,没有一个显示出明确的解决方案(不覆盖组件),而我无法编写,有人可以帮忙吗?

I am not pretty sure I understand your issue, but I tried the example in one of the links and I was able to move focus to next component from code behind exactly as you would do using keyboard. 我不太确定我是否理解您的问题,但是我尝试了其中一个链接中的示例,并且能够像使用键盘一样完全将焦点从代码移到下一个组件。 Here is the code. 这是代码。

<Window x:Class="WpfApplication1.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"
        xmlns:local="clr-namespace:WpfApplication1" Loaded="OnLoaded"
        >
    <StackPanel Margin="10">
        <TextBox Margin="10" x:Name="a" >A</TextBox>
        <TextBox Margin="10" x:Name="b" >B</TextBox>
        <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) {
        a.Focus();
    }

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

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

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