2015-06-20 102 views
1

我有一个RadioButton,它在打开弹出框时被选中。Popup控制键盘

<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打开弹出时,重点在关闭时弹出。(我的项目是键盘底座)文本框。

我使用这段代码来聚焦。

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> 

这是确定的,但打开时弹出RadioButtonNotSave失去了检查,并RadioButtonSave检查!

+0

RadioButtonNotSave lost和RadioButtonSave绑定到“IsSave”属性(我假设用作datacontext的对象的属性)。检查你的代码的哪个部分会在哪些情况下改变该属性,并且你会知道发生了什么。您问题中的触发器似乎与“IsSave”属性的操作无关。 – elgonzo

回答

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> 

在弹出打开你改变对焦点在处理程序:

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

而上弹出引发LostFocus处理您再次更改焦点:

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

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

我知道这是不优雅,其实我尝试了很少有绑定,我没有成功。这是我现在看到的方式。

+0

Tnhaks,tbPopup在打开Popop时会聚焦,但是当我点击进入tbOutsidePopup集中时,Poput中的另一个控件不会聚焦。 –

+0

@ ar.gorgin我以为你想在弹出窗口关闭后立即关注外面的TextBox。 –

+0

是的,我有3个文本框在弹出窗口中,当弹出操作时第一个文本框变得聚焦,但是当按下输入以获得下一个文本框ii去文本框中弹出。 –