[英]Why do the labels on my custom user control cause the mouseleave event to fire?
我已經編寫了一個自定義WPF UserControl。 這是一個帶有名為Base
的Grid
的正方形。 我在該網格上添加了一個橢圓和兩個標簽( volume
和location
),其中填充了從對象屬性中拉出的文本,這些對象是在控件實例化時作為參數給出的。
這是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];
}
}
基本上,當鼠標進入控件時,橢圓的筆划應該發生變化,除非孔已進行操作以使其處於“池化”狀態。
當鼠標進入控件時,它的響應完全符合我的預期: MouseEnter
事件處理程序將觸發。 但是,當用戶將鼠標移到控件內部的標簽之一上時,將觸發MouseLeave
事件。 因此,即使標簽表面上是控件的一部分,下圖也顯示了我在說什么。 打印屏幕會刪除光標,但是我放了藍點來指示光標在哪里:
正確回應:
現在似乎認為鼠標已離開控件:
我嘗試將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">
(我也想發表評論,但聲譽很愚蠢)
好吧,經過大量搜索后發現問題確實存在於另一個控件中。 我還有另一個UserControl
類EmptyWellControl
,它具有Label
。 使用Label
的Height
屬性計算標簽內的文本位置,這將導致一個無意義的值,該值使標簽垂直延伸得遠遠超出窗口的尺寸。 該標簽沒有背景,但是仍然干擾了它穿過的控件。 由於空孔和樣品孔都放置在同一網格上,因此每個SampleWellControl
都會受到這些標簽的影響。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.