2015-06-20 141 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去文本框中彈出。 –