我有一个带按钮和边框的画布:
<Grid>
<Canvas Name="canvas1" >
<Button Name="button1" Visibility="Hidden" />
<Border BorderBrush="Red" BorderThickness="6" Name="border1" />
</Canvas>
</Grid>
现在我想要的是border1的mouseenter,button1应该是可见的,边框本身应该是隐藏的。而在鼠标领域则相反。所以为两个事件编写了以下处理程序:
void border1_MouseLeave(object sender, MouseEventArgs e)
{
border1.Visibility = Visibility.Visible;
button1.Visibility = Visibility.Hidden;
}
void border1_MouseEnter(object sender, MouseEventArgs e)
{
border1.Visibility = Visibility.Hidden;
button1.Visibility = Visibility.Visible;
}
问题是即使我将鼠标指针放在边框上,MouseLeave也会触发。我在这里做错了吗?
答案 0 :(得分:3)
您可能想在边框上尝试背景=“透明”,但我认为最大的问题是您隐藏了边框。我想当你隐藏它时你会得到一个MouseLeave事件,因为你的鼠标不再存在,因为它不再存在了。
答案 1 :(得分:1)
首先,我们通常不会在边框上执行这些事件,因为它通常是一个很薄的元素,如果你移动鼠标太快,你可能会错过这个事件,一般来说你必须准确。 最好在边境内的任何地方进行活动。
第二,如果鼠标输入使边框隐藏,那么应该如何调用MouseLeave方法?! 它不再出现了!
如果单击按钮会使边框再次出现会更有意义。 试试这个:
<Grid>
<Canvas Name="canvas1" >
<Button Width="100" Content="button" Height="100" Name="button1" Visibility="Hidden" Click="button1_Click" />
<Border BorderBrush="Red" BorderThickness="6" Name="border1" Canvas.Left="194" Canvas.Top="22" Height="78" Width="111" MouseEnter="border1_MouseEnter" />
</Canvas>
</Grid>
void border1_MouseEnter(object sender, MouseEventArgs e)
{
border1.Visibility = Visibility.Hidden;
button1.Visibility = Visibility.Visible;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
border1.Visibility = System.Windows.Visibility.Visible;
button1.Visibility = System.Windows.Visibility.Hidden;
}