我正在研究C#WPF項目。 MainWindows只保存用於導航到不同頁面的Frame Control
。我們使用Button
來顯示PopUp
。現在我想在鼠標離開Popup時隱藏這個Popup,並在Page
之上移動。WPF頁面上的鼠標事件 - 僅在子控件上?
<Page x:Class="Name.SpaceMainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
Title="MainPage"
MouseMove="Page_MouseMove">
<StackPanel>
<Label Content="Some Label"/>
<Label Content="Other Label"/>
<Button Name="PopupButton" Content="Popup" Click="MenuButtonClick" />
<Label Content="Third Label"/>
<Popup Name="ExtrasPopup" PlacementTarget="{Binding ElementName=PopupButton}" Placement="Top" PopupAnimation="Scroll">
<StackPanel Background="LightGray">
...
</StackPanel>
</Popup>
</Grid>
</Page>
public partial class MainPage : Page {
...
private void MenuButtonClick(object sender, RoutedEventArgs e) {
ExtrasPopup.IsOpen = true;
}
private void Page_MouseMove(object sender, MouseEventArgs e) {
System.Diagnostics.Debug.WriteLine("Move: " + e.MouseDevice.GetPosition(this));
}
}
問題:僅執行的Page_MouseMove當鼠標移動上述的子控件(標籤或按鈕)。如果鼠標移動到頁面的空白區域之上,則該事件不會觸發。
對於任何其他鼠標事件(如MouseDown,MouseEnter,MouseLeave等)也是如此......這是非常奇怪的行爲。這是打算還是我犯了錯誤?
嘗試'<頁面背景= 「透明」'。鼠標事件僅在背景不爲空時觸發。 – LPL
@LPL非常感謝!這是解決方案! –