我們有幾個彈出窗口看起來像這樣的:打開的窗口或上下文菜單中的某個地方,使彈出的元素unfocusable(除按鈕)
<Popup Name="Popup1" AllowsTransparency="True" Placement="Center" PlacementTarget="{Binding ElementName=FirstGrid}" StaysOpen="True">
<i:Interaction.Behaviors>
<behavior:MouseDragPopupBehavior/>
</i:Interaction.Behaviors>
<Popup.Style>
<Style TargetType="{x:Type Popup}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsPopupVisible}" Value="True">
<Setter Property="IsOpen" Value="True"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding IsPopupVisible}" Value="False">
<Setter Property="IsOpen" Value="False"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Popup.Style>
<ItemsControl ItemsSource="{Binding CenterView}" Grid.Row="2">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Popup>
基本上,這是可拖動的彈出窗口,以及其可見被綁定到視圖模型。 ItemsPanel用於添加UserControls(或者確切地說,綁定到Views的ViewModels)。這工作通常很好。
問題是當我們打開一個上下文菜單或另一個窗口的其他窗口。這將重點放在該窗口或菜單上,我們可以再次關注窗口的唯一方法就是關閉它並重新打開它。
編輯:我忘了提,聚焦窗口是不完全的問題,而是聚焦在彈出窗口中的元素:textbox
等,沒有問題,但是buttons
!
我們設法通過使用我們綁定到ItemsControl的每個查看此代碼來解決與Windows 10和8.1此問題:
private void Grid_MouseEnter(object sender, MouseEventArgs e)
{
if(!Application.Current.MainWindow.IsActive)
{
Application.Current.MainWindow.Activate();
this.Focus();
}
}
,但它仍然沒有在我們的測試工作的Windows 8或Windows 7電腦,奇怪!
有什麼建議嗎?
嘗試使用'FocusManager.SetFocusedElement()'而不是'this.Focus()'。 – AnjumSKhan
@AnjumSKhan這不起作用,謝謝 – Tyress
Downvotes,爲什麼?嘖。 – Tyress