我在布局中使用IsKeyboardFocusWithin值來設定ListViewItem的IsSelected。 到目前為止,它作業得很好,直到我發現,我的專案中的彈出視窗沒有將 IsKeyboardFocus 值傳遞給它的父級,使得這個屬性在彈出視窗的祖先中被設定為假,而彈出視窗和它的子代卻將它設定為真。
當你點擊某些 Popup 的子代(如 Button 或 ListBoxItem)時,這個問題會重現。
我創建了一個簡單的專案來重現這個問題:
當你點擊Popup的一些子專案如Button或ListBoxItem時,這個問題就會重現。
當你點擊按鈕時,IsKeyboardFocusWithin在按鈕和其父級Popup中設定為true,但沒有進一步的設定。
是否有辦法讓Popup的祖先接收其IsKeyboardFocusWithin的值?
<Window。
x:Class="PopupDemo.MainWindow"/span>。
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"/span>
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"。
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"。
xmlns:local="clr-namespace:PopupDemo"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"/span>
Title="MainWindow"。
Width="800"/span>
高度="450"/span>
mc:Ignorable="d"/span>>/span>
<Grid>/span>
< Popup IsOpen="True" StaysOpen="True">
<Button Content="Test" />
</Popup>
</Grid>/span>
<Window.Style>/span>
<Style TargetType="{x:Type Window}"/span>>
<Style.Triggers>>
< 觸發器 屬性="IsKeyboardFocusWithin" 值="True">
< Setter Property="Background" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>>
</Window.Style>/span>
</Window>/span>
uj5u.com熱心網友回復:
Popup, ToolTip, ContextMenu - 它們創建了自己的小視窗。 因此,這些元素不是它們所創建的那個視窗的孩子。 當一個帶有這些元素的視窗彈出時,焦點會轉移到這個視窗。 既然焦點已經轉移到它身上,那么,相應地,它就不在主視窗中了。
在你的調和中,這可以通過一個額外的觸發器來解決,該觸發器決定了彈出視窗的顯示。
但是這樣的解決方案對于任務來說是非常特殊的,可能不適合另一個任務。
例如,對于ListView、ListBox--這將不起作用。
對于它們,你不需要創建一個觸發器,而是改變IsSelected屬性的值。
<Grid>
<Popup x: 名稱="popup" IsOpen="True" StaysOpen="True">
<Button Content="Test" />
</Popup>
</Grid>/span>
<Window.Style>/span>
<Style TargetType="{x:Type Window}"/span>>
<Style.Triggers>>
< 觸發器 屬性="IsKeyboardFocusWithin" 值="True">
< Setter Property="Background" Value="Red" />
</Trigger>
< DataTrigger Binding="{Binding IsKeyboardFocusWithin, ElementName=popup}" Value="True">
< Setter Property="Background" Value="Red" />
</DataTrigger>
</Style.Triggers>/span>
</Style>>
</Window.Style>/span>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/307888.html
標籤:
