如何在给定点附近显示弹出窗口?

时间:2011-11-25 16:25:33

标签: c# wpf popup

我有Point.I想在Point附近显示Popup。有没有设置它的属性?我在这里发现了类似的问题,但它们不是我想要的

1 个答案:

答案 0 :(得分:4)

这是一个例子。只要在窗口上单击鼠标,就会移动弹出窗口:

<强> 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;
}