2013-05-20 69 views
1

当我试图说明FocusVisualStyle只能通过键盘(选项卡和箭头键)激活时才会发生。如何以编程方式显示FocusVisualStyle?

尽量让要应用的FocusVisualStyle组件加载后,这是不可能的事,有一个简单的方法来解决这个问题呢?

我发现这一点:
- focus visual not showing when navigating focus programically
- How do WPF buttons decide to show FocusVisualStyle?
- http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/99856840-f8ef-4547-9150-c4c46ec2f3df

但没有显示一个明确的解决方案(无覆盖部分),我不能写,有人可以帮忙吗?

回答

0

我不肯定我理解你的问题,但我想在链接的一个例子,我能够从代码将焦点移到下一个组件正是背后,你会做使用键盘。这是代码。

<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); 
    } 
} 
+1

尝试对应用的FocusVisualStyle组件加载后,这是不可能做到的,这是我的问题 –

+0

我误解你的问题。现在我看到你的问题。真的很抱歉。 – Jatin