弹出控制键盘

时间:2015-06-20 09:14:35

标签: c# wpf popup

我有一个RadioButton,在检查时打开弹出窗口。

{{1}}

我希望在打开弹出窗口时关注PopupTextBox,在关闭弹出窗口时关注TextBox。(我的项目是键盘基础)。

我使用此代码进行聚焦。

{{1}}

和xaml

{{1}}

没关系,但是当打开弹出的RadioButtonNotSave丢失检查并且RadioButtonSave被检查时!!!

1 个答案:

答案 0 :(得分:0)

我尝试过这种情况:

<Window x:Class="RadioButtonsStack.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">
<Grid>
    <StackPanel>
        <StackPanel.Resources>
            <Style TargetType="{x:Type RadioButton}">
                <EventSetter Event="Click" Handler="EventSetter_OnHandler"/>
            </Style>
        </StackPanel.Resources>
        <RadioButton x:Name="RadioBtn" Content="TestPopup"/>
        <Popup x:Name="myPopup" IsOpen="{Binding IsChecked, ElementName=RadioBtn, Mode=OneWay}" Placement="Mouse" StaysOpen="False">
            <Border Background="LightBlue">
                <TextBox x:Name="tbPopup" Text="inside popup"/>
            </Border>
            <Popup.Style>
                <Style TargetType="Popup">
                    <EventSetter Event="LostFocus" Handler="myPopup_LostFocus"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=myPopup, Path=IsOpen}" Value="False">
                            <Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName=tbOutsidePopup}"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Popup.Style>
        </Popup>
        <TextBox x:Name="tbOutsidePopup" Margin="20" Text="outside popup"/>
    </StackPanel>
</Grid>

代码隐藏:

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

    private void EventSetter_OnHandler(object sender, RoutedEventArgs e)
    {
        myPopup.IsOpen = true;
        tbPopup.Focusable = true;
        Keyboard.Focus(tbPopup);
    }

    private void myPopup_LostFocus(object sender, RoutedEventArgs e)
    {
        tbOutsidePopup.Focusable = true;
        Keyboard.Focus(tbOutsidePopup);
    }
}

你基本上可以从这里得到的OutsideTextBox开始:

<DataTrigger Binding="{Binding ElementName=myPopup, Path=IsOpen}" Value="False">
     <Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName=tbOutsidePopup}"/>
</DataTrigger>

打开Popup时,您可以更改处理程序中的焦点:

tbPopup.Focusable = true;
Keyboard.Focus(tbPopup);

在Popup LostFocus处理程序中,您再次更改焦点:

<EventSetter Event="LostFocus" Handler="myPopup_LostFocus"/>

tbOutsidePopup.Focusable = true;
Keyboard.Focus(tbOutsidePopup);

我知道它并不优雅,事实上我在那里尝试了一些绑定而且我没有成功。这就是我现在看到的方式。