弹出控制键盘

本文关键字:键盘 控制 | 更新日期: 2023-09-27 17:57:09

我有一个单选按钮,选中它时会打开弹出窗口。

<StackPanel>
 <RadioButton x:Name="RadioButtonSave" IsChecked="{Binding IsSave}">Save</RadioButton>
 <RadioButton x:Name="RadioButtonNotSave"  IsChecked="{Binding IsSave,Converter={StaticResource ToNegativeConverter}}">Not Save</RadioButton>
</StackPanel>
<Popup x:Name="Popup" IsOpen="{Binding IsChecked,ElementName=RadioButtonNotSave}" StaysOpen="False" Placement="Left" PlacementTarget="{Binding ElementName=RadioButtonNotSave}">  
 <StackPanel>
  <TextBox x:Name="PopupTextBox" />
 </StackPanel>
</Popup>
<TextBox x:Name="TextBox" />

我想在打开弹出窗口时聚焦 PopupTextBox,在关闭弹出窗口时聚焦 TextBox。(我的项目是键盘底座)。

我使用此代码进行焦点。

public class SetFocusTrigger : TargetedTriggerAction<Control>
{
 protected override void Invoke(object parameter)
  {
    if (Target == null) return;
    Target.Focus();
  }
}

在 XAML 中

<i:Interaction.Triggers>
    <i:EventTrigger EventName="Opened">
        <local:SetFocusTrigger TargetName="PopupTexBox"/>
    </i:EventTrigger>
    <i:EventTrigger EventName="Closed">
        <local:SetFocusTrigger TargetName="TexBox"/>
    </i:EventTrigger>
</i:Interaction.Triggers>

没关系,但是当打开弹出窗口时,选中单选按钮不保存丢失并选中单选按钮保存!!

弹出控制键盘

我已经尝试过这种情况:

<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>

打开弹出窗口后,您将更改处理程序中的焦点:

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

在弹出LostFocus处理程序上,您可以再次更改焦点:

<EventSetter Event="LostFocus" Handler="myPopup_LostFocus"/>
tbOutsidePopup.Focusable = true;
Keyboard.Focus(tbOutsidePopup);

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