我在画布中有一个Thumb(包含一个TextBlock)和一个Border。 Thumb接收拖动事件,并正在正确拖动。
我需要的是在拖动拇指时捕获边框上的MouseEnter事件。
问题是DragCompleted事件(释放鼠标按钮后)触发MouseEnter。 如何在鼠标按钮关闭时鼠标何时进入边框?
的Xaml:
<Grid>
<Canvas Margin="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Canvas Name="dragTarget">
<Thumb
DragStarted="Thumb_DragStarted"
DragDelta="Thumb_DragDelta"
DragCompleted="Thumb_DragCompleted">
<Thumb.Template>
<ControlTemplate>
<TextBlock
Text="Some Text"
Foreground="White"
Background="DimGray"/>
</ControlTemplate>
</Thumb.Template>
</Thumb>
</Canvas>
<Border Width="100" Height="100" Margin="50,50,0,0"
BorderBrush="AliceBlue" BorderThickness="1" Background="Silver"
MouseEnter="Border_MouseEnter"/>
</Canvas>
Xaml.cs:
bool isDragging = false;
double x = 0;
double y = 0;
private void Border_MouseEnter(object sender, MouseEventArgs e)
{
if(isDragging)
Title = "Captured";
}
private void Thumb_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
isDragging = true;
x = e.HorizontalOffset;
y = e.VerticalOffset;
}
private void Thumb_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
{
isDragging = false;
}
private void Thumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
{
x += e.HorizontalChange;
y += e.VerticalChange;
dragTarget.Margin = new Thickness(x , y , 0, 0);
}
答案 0 :(得分:1)
拖动时捕获鼠标,因此您可能无法收到消息。
在Thumb_DragDelta事件中,使用边框获取鼠标位置。它将相对于边界。检查鼠标点是否在边框内。
Point point = Mouse.GetPosition(border);
Rect rect = new Rect(0, 0, border.ActualWidth, border.ActualHeight);
Boolean mouseInBorder = rect.Contains(point);
if (mouseInBorder && !mouseLastInBorder)
{
// Mouse enter "event";
}
if (!mouseInBorder && mouseLastInBorder)
{
// Mouse exit "event";
}
mouseLastInBorder = mouseInBorder;