배경 영역을 클릭하면 WPF 팝업이 닫히는 이유는 무엇입니까?
문제
나는 WPF가있다 Popup
일부 편집 컨트롤 (목록 상자, 텍스트 상자, 확인란)이 포함 된 컨트롤.
Popup.StaysOpen
설정되었습니다 False
, 필요한. 사용자가 애플리케이션의 다른 곳을 클릭하면 편집 작업이 중단되고 팝업이 닫혀야합니다.
불행히도 팝업은 팝업의 배경 영역 내에서 사용자가 클릭 할 때마다 팝업이 닫힙니다 (편집 컨트롤 사이의 공간).
팝업을 설정하려고 시도했습니다 Focusable
. 나는 또한 팝업의 아이를 설정하려고 시도했습니다 (a Border
) 집중할 수 있습니다. 어느 쪽이든 운이 없습니다.
또한, 마우스 이벤트는 팝업을 통해 터널링하는 것 같습니다. 클릭하면 팝업 아래에있는 요소가 집중됩니다. 이것은 둘 다에도 불구하고입니다 Popup
그리고 Border
(내가 클릭하는 곳) 둘 다가 있습니다 IsHitTestVisible
그리고 Focusable
로 설정 true
.
해결책
결국, 나는 다음이 효과가 있음을 발견했습니다. 주어진...
<Popup x:Name="_popup"
StaysOpen="False"
PopupAnimation="Slide"
AllowsTransparency="True">
... 전화 후이 코드를 생성자에 사용했습니다. InitializeComponent
...
// Ensure that any mouse event that gets through to the
// popup is considered handled, otherwise the popup would close
_popup.MouseDown += (s, e) => e.Handled = true;
다른 팁
팝업과 테두리에 초점을 맞출 수 있다는 것은 이상하게 보입니다. 마우스가 국경 위에있을 때 방아쇠에서 Staysopen을 변경하여 문제를 해결할 수있었습니다.
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ToggleButton x:Name="btnPop" Content="Pop!" Width="100" Height="50"/>
<Popup Placement="Bottom" PlacementTarget="{Binding ElementName=btnPop}" IsOpen="{Binding IsChecked, ElementName=btnPop}">
<Popup.Style>
<Style TargetType="{x:Type Popup}">
<Setter Property="StaysOpen" Value="False"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsMouseOver, ElementName=brd}" Value="True">
<Setter Property="StaysOpen" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Popup.Style>
<Border x:Name="brd" Background="White" BorderThickness="1" BorderBrush="Black">
<StackPanel>
<TextBox Margin="10"/>
<TextBlock Text="Some text is here." Margin="10"/>
<TextBox Margin="10"/>
</StackPanel>
</Border>
</Popup>
</Grid>
내 최선의 추측은 당신이 몇 가지 투명성 문제가 진행되고 있다는 것입니다. 팝업에 배경 브러시를 설정해보십시오.
토글 부튼이나 다른 종류의 버튼에 팝업이 중첩되지 않습니까? 그런 다음 팝업 레벨에서 라우팅 된 이벤트를 중지하는 것은 논리적입니다.
당신은 설정할 수 있습니다 숙박= 팝업의 Mouseleave Event Timer.start ()에서 3 초 후이 팝업을 닫습니다. Timer.stop () 에서이 팝업을 닫습니다. 작동합니다.