c# - 为什么当鼠标悬停在 Wpf 窗口中的控件上时位置会更改
问题描述
我需要一些 wpf 控件在由 hwndHost 托管的 wpf 控件上,所以我使用子窗口在父窗口中包含这些控件。但是当我将控件悬停在子窗口中时,子窗口的位置会发生变化,并且不会调用活动:LocationChanged。我很困惑,谁能帮帮我~谢谢。
测试1.xaml
<Window x:Class="WpfApplication1.Test1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="Test1" Height="350" Width="525" WindowStyle="None" AllowsTransparency="True">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="52*"/>
<ColumnDefinition Width="153*"/>
<ColumnDefinition Width="54*"/>
</Grid.ColumnDefinitions>
<Border Grid.Column="0" Name="leftListContain" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
<Border Grid.Column="2" Name="rightListContain" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</Grid>
</Window>
测试1.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using System.Threading;
namespace WpfApplication1
{
/// <summary>
/// </summary>
public partial class Test1 : Window
{
ControlList leftControlList = null;
ControlList rightControlList = null;
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetParent")]
public extern static IntPtr SetParent(IntPtr childPtr, IntPtr parentPtr);
public Test1()
{
InitializeComponent();
this.Loaded += ControlListLoadedHandler;
this.SizeChanged += ControlListResizeHandler;
}
private void ControlListLoadedHandler(object sender, EventArgs e)
{
if (leftControlList != null && rightControlList != null)
{
return;
}
Thread t = new Thread(() =>
{
while (!unityControl.isUnityLoaded)
{
Thread.Sleep(10);
}
this.Dispatcher.Invoke(() => {
ControlListLoaded();
});
});
t.Start();
this.WindowState = System.Windows.WindowState.Maximized;
}
private void ControlListLoaded()
{
leftControlList = new ControlList();
leftControlList.Show();
WindowInteropHelper leftParentHelper = new WindowInteropHelper(this);
WindowInteropHelper leftChildHelper = new WindowInteropHelper(leftControlList);
SetParent(leftChildHelper.Handle, leftParentHelper.Handle);
rightControlList = new ControlList();
rightControlList.Show();
WindowInteropHelper rightParentHelper = new WindowInteropHelper(this);
WindowInteropHelper rightChildHelper = new WindowInteropHelper(rightControlList);
SetParent(rightChildHelper.Handle, rightParentHelper.Handle);
ControlListResize();
}
private void ControlListResizeHandler(object sender, EventArgs e)
{
ControlListResize();
}
private void ControlListResize()
{
if (leftControlList == null || rightControlList == null)
{
return;
}
leftControlList.SetSize(leftListContain.ActualWidth, leftListContain.ActualHeight);
Point point = leftListContain.TransformToAncestor(this).Transform(new Point(0, 0));
leftControlList.SetPosition(point.X, point.Y);
rightControlList.SetSize(rightListContain.ActualWidth, rightListContain.ActualHeight);
point = rightListContain.TransformToAncestor(this).Transform(new Point(0, 0));
rightControlList.SetPosition(point.X, point.Y);
}
}
}
控件列表.xaml
<Window x:Class="WpfApplication1.ControlList"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300" WindowStyle="None" Opacity="0.5" AllowsTransparency="True">
<Grid Name="grid">
<ListBox Name="listBox" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<ListBoxItem>11111111111111111111111111</ListBoxItem>
<ListBoxItem>11111111111111111111111111</ListBoxItem>
<ListBoxItem>11111111111111111111111111</ListBoxItem>
<ListBoxItem>11111111111111111111111111</ListBoxItem>
<ListBoxItem>11111111111111111111111111</ListBoxItem>
<ListBoxItem>11111111111111111111111111</ListBoxItem>
<ListBoxItem>11111111111111111111111111</ListBoxItem>
<ListBoxItem>11111111111111111111111111</ListBoxItem>
<ListBoxItem>11111111111111111111111111</ListBoxItem>
<ListBoxItem>11111111111111111111111111</ListBoxItem>
<ListBoxItem>11111111111111111111111111</ListBoxItem>
</ListBox>
</Grid>
</Window>
控制.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
namespace WpfApplication1
{
/// <summary>
/// </summary>
public partial class ControlList : Window
{
public ControlList()
{
InitializeComponent();
}
public void SetSize(double width, double height)
{
this.Width = width;
this.Height = height;
}
public void SetPosition(double left, double top)
{
this.Left = left;
this.Top = top;
}
}
}
解决方案
推荐阅读
- jenkins - Sonarqube 扫描以“INFO:ANALYSIS SUCCESSFUL”结束,但 sonarqube UI 显示“项目尚未分析”。
- google-apps-script - 根据文件和文件夹名称中的通用字符串将 Google Docs 分类到 Google Drive 文件夹中
- android - 使用 AsyncTask 和 POST 方法将变量发送到服务器并返回 JSON 对象
- database - Aurora Writer 和 Reader 内部复制(replica lag)时间
- javascript - 浏览器搜索栏关闭后如何将焦点设置为元素?
- android - 如何在 Android 中读取文本文件?
- vb.net - 打印没有数据集的 DataGridView:Xsd
- javascript - 检查对象值是否大于 0
- php - XMPP 重新协商流
- c++ - QML 函数总是返回空白