There were a few old questions about this but nothing looked like a definitive answer. I have a EditorSearchView in a DockPanel. When a command is executed I show/hide the search view. When it is shown I'd like to throw focus to the 'Find what' text box. Calling .Focus() doesn't seem to work. Here's an example program.
using System.Windows;
using System.Windows.Input;
namespace ActiproTest
{
public partial class MainWindow : Window
{
public static RoutedUICommand FindCommand = new RoutedUICommand(
"Find",
"Find",
typeof(MainWindow),
new InputGestureCollection(new KeyGesture[] { new KeyGesture(Key.F, ModifierKeys.Control, "F") }));
public MainWindow()
{
InitializeComponent();
}
private void OnFind(object sender, RoutedEventArgs e)
{
if (search.IsVisible)
{
search.Visibility = System.Windows.Visibility.Collapsed;
}
else
{
search.Visibility = System.Windows.Visibility.Visible;
search.Focus();
}
e.Handled = true;
}
}
}
<Window
x:Class="ActiproTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:acti="http://schemas.actiprosoftware.com/winfx/xaml/syntaxeditor"
xmlns:local="clr-namespace:ActiproTest"
Title="MainWindow" Height="350" Width="525">
<Window.CommandBindings>
<CommandBinding Command="local:MainWindow.FindCommand" Executed="OnFind"/>
</Window.CommandBindings>
<DockPanel LastChildFill="True">
<Button
Command="local:MainWindow.FindCommand"
Width="16"
DockPanel.Dock="Right"></Button>
<acti:EditorSearchView
x:Name="search"
DockPanel.Dock="Right"/>
<acti:SyntaxEditor/>
</DockPanel>
</Window>