如何显示弹出在附近的给定点

本文关键字:在附近 何显示 显示 | 更新日期: 2023-09-27 18:15:59

我有点。我想在Point附近显示Popup。有什么属性可以设置它吗?我在这里发现了类似的问题,但它们不是我想要的

如何显示弹出在附近的给定点

下面是一个例子。当你在一个窗口上点击鼠标时,弹出窗口将被移动:

XAML:

<Window x:Class="WpfApplication60.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Name="myWindow"
        Title="MainWindow" Height="350" Width="525" MouseLeftButtonDown="Window_MouseLeftButtonDown">
    <Grid>
    <Popup x:Name="myPopup" PlacementTarget="{Binding ElementName=myWindow}" Placement="Relative">
      <Label Width="50" Height="20" Background="LightGreen"  MouseLeftButtonDown="Label_MouseLeftButtonDown">I am the Popup</Label>
    </Popup>
  </Grid>
</Window>

代码:

private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
  Point current = e.GetPosition(this);
  myPopup.HorizontalOffset = current.X;
  myPopup.VerticalOffset = current.Y;
  myPopup.IsOpen = true;
}