我使用MouseEnter
事件进行简单的边框控制,打开弹出控件。弹出窗口比边界更大,因此它超出了边界的范围。弹出窗口还有一些按钮和文本块,所以我需要对它进行一些控制。现在,我想要获得:鼠标进入边框 - 弹出窗口显示。鼠标离开边框和弹出窗口 - 然后弹出窗口关闭。因此,当鼠标不在边界上但超过弹出窗口时 - 弹出窗口保持打开状态。我不想点击关闭。你能帮助我吗? WPF中的弹出控件对我来说很困惑。
编辑: 对,代码:
<Window x:Class="Sandbox.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Sandbox"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid Margin="20">
<Popup PopupAnimation="Fade" Placement="Top" PlacementTarget="{Binding ElementName=OpenPopupBorder}" AllowsTransparency="True" StaysOpen="True" x:Name="DeviceStatusSnippetPopup">
<Border Background="White" Padding="5" BorderBrush="Black" BorderThickness="1">
<Grid>
<StackPanel Orientation="Vertical">
<TextBlock HorizontalAlignment="Center" Text="Device status snippet" Margin="0 10 0 10"/>
<StackPanel Orientation="Horizontal">
<Button Style="{StaticResource btn-default}" Margin="0 0 5 0" Content="Go to device"/>
<Button Style="{StaticResource btn-default}" Content="Refresh device"/>
</StackPanel>
</StackPanel>
</Grid>
</Border>
</Popup>
<Border x:Name="OpenPopupBorder" Width="90" Height="30" Background="LightGray" MouseEnter="Border_MouseEnter"/>
</Grid>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Border_MouseEnter(object sender, MouseEventArgs e)
{
DeviceStatusSnippetPopup.IsOpen = true;
}
}
答案 0 :(得分:0)
代码隐藏:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Border_MouseEnter(object sender, MouseEventArgs e)
{
DeviceStatusSnippetPopup.IsOpen = true;
}
private void Popup_MouseLeave(object sender, MouseEventArgs e)
{
if (!OpenPopupBorder.IsMouseOver)
DeviceStatusSnippetPopup.IsOpen = false;
}
private void Border_MouseLeave(object sender, MouseEventArgs e)
{
if (!DeviceStatusSnippetPopup.IsMouseOver)
DeviceStatusSnippetPopup.IsOpen = false;
}
}
XAML(减去窗口):
<Grid Margin="20">
<Popup MouseLeave="Popup_MouseLeave" PopupAnimation="Fade" Placement="Top" PlacementTarget="{Binding ElementName=OpenPopupBorder}" AllowsTransparency="True" StaysOpen="True" x:Name="DeviceStatusSnippetPopup">
<Border Background="White" Padding="5" BorderBrush="Black" BorderThickness="1">
<Grid>
<StackPanel Orientation="Vertical">
<TextBlock HorizontalAlignment="Center" Text="Device status snippet" Margin="0 10 0 10"/>
<StackPanel Orientation="Horizontal">
<Button Margin="0 0 5 0" Content="Go to device"/>
<Button Content="Refresh device"/>
</StackPanel>
</StackPanel>
</Grid>
</Border>
</Popup>
<Border x:Name="OpenPopupBorder" Width="90" Height="30" Background="LightGray" MouseEnter="Border_MouseEnter" MouseLeave="Border_MouseLeave"/>
</Grid>
基本上,你为边框和弹出窗口实现MouseLeave的事件处理程序,并在关闭弹出窗口之前检查鼠标是否在另一个上面。