wpf-简单悬浮窗的定位问题

当前效果:移入textblock范围内时浮窗出现,移出时消失。
wpf-简单悬浮窗的定位问题_第1张图片
前台

	<Grid x:Name="mainGrid" ShowGridLines="True">
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="*"/>
        Grid.RowDefinitions>
        
        <Grid Grid.Row="0">
            <TextBlock Text="测试浮窗" 
                       MouseMove="TextBlock_MouseMove"
                       MouseLeave="TextBlock_MouseLeave"
                       Margin="10, 20, 10,10"
                       Width="50"/>
                       
            <Popup x:Name="popPath" AllowsTransparency="True">
                <Border BorderBrush="Transparent" BorderThickness="0" Background="Transparent">
                    <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
                        <Path Stroke="Black" StrokeThickness="1" Fill="White" Data="M 0,0 L 120,0 L 120,30 L 65,30 L 60,35 L 55,30 L 0,30 Z"/>
                        <Grid x:Name="grpopPath" Margin="5,5,5,10">
                            <TextBlock x:Name="tbPath" Foreground="Black" FontSize="13" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        Grid>
                    Grid>
                Border>
            Popup>
            
        Grid>
    Grid>

后台

        private void TextBlock_MouseMove(object sender, MouseEventArgs e)
        {
            TextBlock t = (TextBlock)sender;
            tbPath.Text = t.Text;
			// 位置
            Point p = e.GetPosition(mainGrid);
            popPath.IsOpen = true;
            popPath.HorizontalOffset = p.X - 60;
            popPath.VerticalOffset = p.Y -100;
        }
        private void TextBlock_MouseLeave(object sender, MouseEventArgs e)
        {
            popPath.IsOpen = false;
        }

你可能感兴趣的:(c#和WPF,wpf)