更改父网格鼠标悬停WPF上的椭圆属性

时间:2014-12-11 18:13:54

标签: wpf wpf-controls wpf-4.0

我有一个网格,其中我有一个椭圆,我希望鼠标在gid上方,椭圆应该用红色填充。我怎么能这样做?如果我将触发器放在椭圆中,那么它只能在鼠标上用椭圆形覆盖。但我希望颜色应该改变o鼠标在椭圆的父网格上结束。

以下是我的用户控制代码:

    <UserControl x:Class="DeviceIcon"
         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="31" d:DesignWidth="31">


<Grid Background="#00000000" Name="MainGrid">
    <Ellipse Name="BackCircle" Width="30" Height="30" Fill="Lavender" Stroke="Red" >

    </Ellipse>
</Grid>

1 个答案:

答案 0 :(得分:0)

您可以将事件处理程序添加到父网格。鼠标事件从椭圆和其他子元素冒泡到网格。

<强> XAML

<Grid Background="#00000000"
      Name="MainGrid"
      MouseEnter='MainGrid_MouseEnter'
      MouseLeave='MainGrid_MouseLeave'>

    <Ellipse Name="BackCircle"
         Width="30"
         Height="30"
         Fill="Lavender"
         Stroke="Red">

    </Ellipse>
    </Grid>

<强>代码

   private Brush _oldFill;
   private void MainGrid_MouseEnter(object sender, MouseEventArgs e) {
      _oldFill = BackCircle.Fill;
      BackCircle.Fill = new SolidColorBrush(Colors.Red);
        }

    private void MainGrid_MouseLeave(object sender, MouseEventArgs e) {
      BackCircle.Fill = _oldFill;
     }