gpt4 book ai didi

c# - 为什么自定义用户控件上的标签会触发 mouseleave 事件?

转载 作者:太空宇宙 更新时间:2023-11-03 15:52:53 25 4
gpt4 key购买 nike

我编写了自定义 WPF UserControl。它是一个带有名为 BaseGrid 的正方形。在该网格中,我添加了一个椭圆和两个标签(volumelocation),其中填充了从对象属性中提取的文本,该对象在控制时作为参数给出实例化。

这是 XAML:

<UserControl x:Class="EasyHyb.SampleWellControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="100" d:DesignWidth="100">
<Grid x:Name="Base">
</Grid>
</UserControl>

以及代码隐藏中的构造函数/事件函数:

public SampleWellControl(int size, SourceSample sample)
{
InitializeComponent();
this.sample = sample;
this.Width = this.Height = size;
this.selected = SelectionStatus.Unselected;

double spacing = size / 4;

volume = new Label();
location = new Label();

volume.Content = String.Format("{0:0.00}", sample.volume);
location.Content = sample.well.well;

volume.HorizontalAlignment = location.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
volume.FontFamily = location.FontFamily = new System.Windows.Media.FontFamily("Meiryo UI");
volume.FontWeight = location.FontWeight = FontWeights.Bold;
volume.Background = location.Background = Base.Background = this.Background = Brushes.Transparent;
volume.Margin = new Thickness(0, spacing, 0, 0);
location.Margin = new Thickness(0, spacing * 2, 0, 0);

well = new Ellipse();
well.Width = well.Height = this.Width;
well.StrokeThickness = 3;

Base.Children.Add(well);
Base.Children.Add(volume);
Base.Children.Add(location);

this.MouseEnter += SampleWellControl_MouseEnter;
this.MouseLeave += SampleWellControl_MouseLeave;
this.MouseUp += SampleWellControl_MouseUp;

this.Cursor = Cursors.Hand;

UpdateFillAndStroke();
}

void SampleWellControl_MouseLeave(object sender, MouseEventArgs e)
{
RevertWell();
}

void SampleWellControl_MouseEnter(object sender, MouseEventArgs e)
{
HighlightWell();
}

public void HighlightWell()
{
if (this.selected == SelectionStatus.Pooled)
{
return;
}
if (this.selected == SelectionStatus.Unselected)
{
this.well.Stroke = this.strokes[SelectionStatus.Selected];
}
else
{
this.well.Stroke = this.strokes[SelectionStatus.Unselected];
}
}

public void RevertWell()
{
if (this.selected == SelectionStatus.Pooled)
{
return;
}
if (this.selected == SelectionStatus.Unselected)
{
this.well.Stroke = this.strokes[SelectionStatus.Unselected];
}
else
{
this.well.Stroke = this.strokes[SelectionStatus.Selected];
}
}

基本上,当鼠标进入控件时,椭圆的笔划应该发生变化,除非该井经过操作使其处于“Pooled”状态。

当鼠标进入控件时,它的响应完全符合我的预期:MouseEnter 事件处理程序触发。但是,当用户将鼠标移到控件内的某个标签上时,MouseLeave 事件会触发。所以即使标签表面上是控件的一部分下面的图片显示了我在说什么。 Print Screen 删除了光标,但我用蓝点表示光标所在的位置:

正确回应:

Responding properly

现在好像认为鼠标离开了控件:

Not responding properly

我试过向标签添加 MouseEnter 和 MouseLeave 事件处理程序,但它们没有触发。当鼠标悬停在标签上时,光标也会从手形变为指针。在另一个类中实例化后,我尝试将 MouseEnter 和 MouseLeave 事件处理程序添加到控件中。我为网格、控件和标签添加了透明背景,但这也没有任何区别。

我还检查了我的 MouseLeave 事件处理程序以查看鼠标是否在控件上,似乎控件没有检测到光标在控件本身上:

if(!this.IsMouseOver)
{
RevertWell();
}
//also tried IsMouseDirectlyOver

我希望 MouseLeave 仅在光标离开控件的方形边界时触发。如何在保留标签的同时完成此操作?

最佳答案

结合使用

IsHitTestVisible="False"

在添加到 Base 的所有对象上:

volume = new Label();
volume.IsHitTestVisible="False";

然后你的容器有事件,给出背景

<Grid x:Name="Base" Background="White">

(我也想发表评论,但声誉很愚蠢)

关于c# - 为什么自定义用户控件上的标签会触发 mouseleave 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25067201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com