WPF中鼠标下A类控件的计数
我在画布上有一些自定义的 A 面板,其中还有 B 面板,我如何计数实际位于鼠标光标的 A 面板?
我知道这可以通过 VisualTreeHelper.HitTest 来实现,但机会不大,它总是返回自定义面板上的元素,或者根本不返回任何内容...
这是我的
<UserControl x:Class="WpfApplication7.UserControl1">
<Grid>
<Label Content="Label" Height="44" HorizontalAlignment="Left" Name="label1" VerticalAlignment="Top" FontSize="20" FontWeight="Bold" Width="78" Background="#FF4B9FC4" BorderBrush="#FF020A0D" BorderThickness="1" />
</Grid>
</UserControl>
<Window x:Class="WpfApplication7.MainWindow"
PreviewMouseLeftButtonDown="Window_PreviewMouseLeftButtonDown" xmlns:my="clr-namespace:WpfApplication7">
<Grid>
<my:UserControl1 HorizontalAlignment="Left" Margin="82,88,0,0" x:Name="userControl11" VerticalAlignment="Top" />
<my:UserControl1 HorizontalAlignment="Left" Margin="168,166,0,0" x:Name="userControl12" VerticalAlignment="Top" />
<my:UserControl1 HorizontalAlignment="Left" Margin="231,130,0,0" x:Name="userControl13" VerticalAlignment="Top" />
</Grid>
</Window>
.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
List<UserControl1> ucs = new List<UserControl1>();
private void Window_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
GetUcsCount(e);
Console.WriteLine("ucs.Count = {0}", ucs.Count);
}
private void GetUcsCount(MouseButtonEventArgs e)
{
ucs.Clear();
Point p = e.GetPosition(this);
VisualTreeHelper.HitTest(this, null,
new HitTestResultCallback(MyHitTestCallback),
new PointHitTestParameters(p));
}
HitTestResultBehavior MyHitTestCallback(HitTestResult result)
{
if (result.VisualHit.GetType() == typeof(UserControl1))
{
ucs.Add(result.VisualHit as UserControl1);
}
return HitTestResultBehavior.Continue;
}
}
代码 结果 == 0
我点击的任何地方...
I have some custom A panels on a canvas, where there a also B panels, how can I count A panels located the mouse cursor actually?
I know that this could be achieved with VisualTreeHelper.HitTest
, but didn't have much chance, it returns always the elements on the custom panels or nothing at all...
this is my code
<UserControl x:Class="WpfApplication7.UserControl1">
<Grid>
<Label Content="Label" Height="44" HorizontalAlignment="Left" Name="label1" VerticalAlignment="Top" FontSize="20" FontWeight="Bold" Width="78" Background="#FF4B9FC4" BorderBrush="#FF020A0D" BorderThickness="1" />
</Grid>
</UserControl>
<Window x:Class="WpfApplication7.MainWindow"
PreviewMouseLeftButtonDown="Window_PreviewMouseLeftButtonDown" xmlns:my="clr-namespace:WpfApplication7">
<Grid>
<my:UserControl1 HorizontalAlignment="Left" Margin="82,88,0,0" x:Name="userControl11" VerticalAlignment="Top" />
<my:UserControl1 HorizontalAlignment="Left" Margin="168,166,0,0" x:Name="userControl12" VerticalAlignment="Top" />
<my:UserControl1 HorizontalAlignment="Left" Margin="231,130,0,0" x:Name="userControl13" VerticalAlignment="Top" />
</Grid>
</Window>
.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
List<UserControl1> ucs = new List<UserControl1>();
private void Window_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
GetUcsCount(e);
Console.WriteLine("ucs.Count = {0}", ucs.Count);
}
private void GetUcsCount(MouseButtonEventArgs e)
{
ucs.Clear();
Point p = e.GetPosition(this);
VisualTreeHelper.HitTest(this, null,
new HitTestResultCallback(MyHitTestCallback),
new PointHitTestParameters(p));
}
HitTestResultBehavior MyHitTestCallback(HitTestResult result)
{
if (result.VisualHit.GetType() == typeof(UserControl1))
{
ucs.Add(result.VisualHit as UserControl1);
}
return HitTestResultBehavior.Continue;
}
}
the result == 0
anywhere I click...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于 HitTestResultCallback 中的每个命中,您可以尝试查找父 UserControl1 并将其添加到列表(如果尚未添加)
For each hit in the HitTestResultCallback you can try to find the parent UserControl1 and add it to the list if it hasn't been added yet