我正在尝试将焦点设置为窗口显示上的特定元素。该窗口只是带有标签的搜索/替换窗口。当用户选择“搜索”时,将激活搜索选项卡,并且搜索文本框应获得焦点。同样,当用户选择“替换”时,替换选项卡被激活,替换文本框应获得焦点。
由于窗口是非模态的,因此我将重用它并隐藏而不是关闭。问题是没有正确接收焦点。说:
在打开的窗口中,根据用户的选择,我调用以下方法之一:
public void ChooseReplaceTab()
{
tcTabs.SelectedItem = tReplace;
FocusManager.SetFocusedElement(this, tbReplaceSearch);
tbReplaceSearch.Focus();
}
public void ChooseSearchTab()
{
tcTabs.SelectedItem = tSearch;
FocusManager.SetFocusedElement(this, tbSearchSearch);
tbSearchSearch.Focus();
}
搜索窗口没有任何花哨的内容,只有一堆标签,文本框,单选按钮和按钮:
<Window x:Class="Dev.Editor.SearchReplaceWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Dev.Editor"
xmlns:p="clr-namespace:Dev.Editor.Resources;assembly=Dev.Editor.Resources"
xmlns:t="clr-namespace:Dev.Editor.BusinessLogic.Types.Search;assembly=Dev.Editor.BusinessLogic"
mc:Ignorable="d"
Title="{x:Static p:Strings.SearchWindow_Title}" SizeToContent="WidthAndHeight" ResizeMode="NoResize" Closing="HandleWindowClosing">
<TabControl x:Name="tcTabs" Margin="{StaticResource DialogWindowPadding}">
<!-- Search -->
<TabItem x:Name="tSearch" Header="{x:Static p:Strings.SearchWindow_SearchTab}" >
<GroupBox Header="{x:Static p:Strings.SearchWindow_SearchTab}" Padding="4">
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Vertical">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="250" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="{x:Static p:Strings.SearchWindow_SearchedText}"/>
<TextBox x:Name="tbSearchSearch" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Stretch" Text="{Binding Search, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</Grid>
<GroupBox Header="{x:Static p:Strings.SearchWindow_SearchOptions}" Padding="4">
<StackPanel Orientation="Vertical">
<CheckBox Margin="{StaticResource DialogItemsVMargin}" Content="{x:Static p:Strings.SearchWindow_CaseSensitive}"
IsChecked="{Binding CaseSensitive, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<CheckBox Margin="{StaticResource DialogItemsVMargin}" Content="{x:Static p:Strings.SearchWindow_OnlyFullWords}"
IsChecked="{Binding WholeWordsOnly, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<CheckBox Content="{x:Static p:Strings.SearchWindow_SearchBackwards}"
IsChecked="{Binding SearchBackwards, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</StackPanel>
</GroupBox>
<GroupBox Header="{x:Static p:Strings.SearchWindow_SearchMode}" Padding="4">
<StackPanel Orientation="Vertical">
<RadioButton Margin="{StaticResource DialogItemsVMargin}" Content="{x:Static p:Strings.SearchWindow_SearchModeNormal}"
IsChecked="{Binding SearchMode, Converter={StaticResource RadioToEnumConverter}, ConverterParameter={x:Static t:SearchMode.Normal}}"/>
<RadioButton Margin="{StaticResource DialogItemsVMargin}" Content="{x:Static p:Strings.SearchWindow_SearchModeExtended}"
IsChecked="{Binding SearchMode, Converter={StaticResource RadioToEnumConverter}, ConverterParameter={x:Static t:SearchMode.Extended}}"/>
<RadioButton Content="{x:Static p:Strings.SearchWindow_SearchModeRegex}"
IsChecked="{Binding SearchMode, Converter={StaticResource RadioToEnumConverter}, ConverterParameter={x:Static t:SearchMode.RegularExpressions}}"/>
</StackPanel>
</GroupBox>
</StackPanel>
<StackPanel Orientation="Vertical" Margin="10,0,0,0">
<Button Content="{x:Static p:Strings.SearchWindow_FindNext}" Width="150" Margin="{StaticResource DialogItemsVMargin}" Command="{Binding FindNextCommand}" />
<Button Content="{x:Static p:Strings.SearchWindow_Close}" Width="150" Command="{Binding CloseCommand}" />
</StackPanel>
</StackPanel>
</GroupBox>
</TabItem>
<TabItem x:Name="tReplace" Header="{x:Static p:Strings.SearchWindow_ReplaceTab}">
<GroupBox Header="{x:Static p:Strings.SearchWindow_ReplaceTab}" Padding="4">
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Vertical">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="250" />
</Grid.ColumnDefinitions>
<Label Margin="{StaticResource DialogItemsVMargin}" Grid.Row="0" Grid.Column="0" Content="{x:Static p:Strings.SearchWindow_SearchedText}"/>
<TextBox x:Name="tbReplaceSearch" Margin="{StaticResource DialogItemsVMargin}" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Stretch"
Text="{Binding Search, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<Label Grid.Row="1" Grid.Column="0" Content="{x:Static p:Strings.SearchWindow_ReplaceWith}" />
<TextBox Grid.Row="1" Grid.Column="1" HorizontalAlignment="Stretch"
Text="{Binding Replace, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</Grid>
<GroupBox Header="{x:Static p:Strings.SearchWindow_SearchOptions}" Padding="4">
<StackPanel Orientation="Vertical">
<CheckBox Margin="{StaticResource DialogItemsVMargin}" Content="{x:Static p:Strings.SearchWindow_CaseSensitive}"
IsChecked="{Binding CaseSensitive, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<CheckBox Margin="{StaticResource DialogItemsVMargin}" Content="{x:Static p:Strings.SearchWindow_OnlyFullWords}"
IsChecked="{Binding WholeWordsOnly, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<CheckBox Content="{x:Static p:Strings.SearchWindow_SearchBackwards}"
IsChecked="{Binding SearchBackwards, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</StackPanel>
</GroupBox>
<GroupBox Header="{x:Static p:Strings.SearchWindow_SearchMode}" Padding="4">
<StackPanel Orientation="Vertical">
<RadioButton Margin="{StaticResource DialogItemsVMargin}" Content="{x:Static p:Strings.SearchWindow_SearchModeNormal}"
IsChecked="{Binding SearchMode, Converter={StaticResource RadioToEnumConverter}, ConverterParameter={x:Static t:SearchMode.Normal}}"/>
<RadioButton Margin="{StaticResource DialogItemsVMargin}" Content="{x:Static p:Strings.SearchWindow_SearchModeExtended}"
IsChecked="{Binding SearchMode, Converter={StaticResource RadioToEnumConverter}, ConverterParameter={x:Static t:SearchMode.Extended}}"/>
<RadioButton Content="{x:Static p:Strings.SearchWindow_SearchModeRegex}"
IsChecked="{Binding SearchMode, Converter={StaticResource RadioToEnumConverter}, ConverterParameter={x:Static t:SearchMode.RegularExpressions}}"/>
</StackPanel>
</GroupBox>
</StackPanel>
<StackPanel Orientation="Vertical" Margin="10,0,0,0">
<Button Content="{x:Static p:Strings.SearchWindow_FindNext}" Width="150" Margin="{StaticResource DialogItemsVMargin}" Command="{Binding FindNextCommand}" />
<Button Content="{x:Static p:Strings.SearchWindow_Replace}" Width="150" Margin="{StaticResource DialogItemsVMargin}" Command="{Binding ReplaceCommand}"/>
<Separator Margin="{StaticResource DialogItemsVMargin}" />
<Button Content="{x:Static p:Strings.SearchWindow_ReplaceAll}" Width="150" Margin="{StaticResource DialogItemsVMargin}" Command="{Binding ReplaceAllCommand}"/>
<CheckBox Content="{x:Static p:Strings.SearchWindow_InSelection}" Margin="{StaticResource DialogItemsVMargin}"
IsEnabled="{Binding SelectionAvailable}" IsChecked="{Binding ReplaceAllInSelection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<Separator Margin="{StaticResource DialogItemsVMargin}" />
<Button Content="{x:Static p:Strings.SearchWindow_Close}" Width="150" Command="{Binding CloseCommand}"></Button>
</StackPanel>
</StackPanel>
</GroupBox>
</TabItem>
</TabControl>
</Window>
调用这些方法时,应该如何正确地将焦点设置到特定元素上?
答案 0 :(得分:2)
您可以尝试以下操作:
Dispatcher.BeginInvoke(
DispatcherPriority.ContextIdle,
new Action(delegate()
{
tbSearchSearch.Focus();
}));