Silverlight 2b2 中自定义控件中子控件MouseEnter事件改变VisualState的问题

 在Silverlight 2 beta2中,开发一个自定义控件ActionSpotButton,该控件包含一个背景矩形rectBackground和一个按钮hotButton。想当用户鼠标移动到Button上时改变背景矩形的颜色。

Silverlight 2b2 中自定义控件中子控件MouseEnter事件改变VisualState的问题_第1张图片      Silverlight 2b2 中自定义控件中子控件MouseEnter事件改变VisualState的问题_第2张图片

 

generic.xaml文件内容如下:

  1. <Style TargetType="local:ActionSpotButton">
  2.         <Setter Property="IsEnabled" Value="True" />
  3.         <Setter Property="Template">
  4.             <Setter.Value>
  5.                 <ControlTemplate TargetType="local:ActionSpotButton">
  6.                     <Canvas x:Name="RootElement" Background="WhiteSmoke">
  7.                         <vsm:VisualStateManager.VisualStateGroups>
  8.                             <vsm:VisualStateGroup x:Name="CommonStates">
  9.                                 <vsm:VisualState x:Name="Normal" >
  10.                                     <Storyboard>
  11.                                         <DoubleAnimation BeginTime="00:00:00" From="0" To="0.5" Duration="0" Storyboard.TargetName="image" Storyboard.TargetProperty="(UIElement.Opacity)" />
  12.                                     </Storyboard>
  13.                                 </vsm:VisualState>
  14.                                 <vsm:VisualState x:Name="MouseOver1">
  15.                                     <Storyboard>
  16.                                         <DoubleAnimation BeginTime="00:00:00" From="0" To="1" Duration="0" Storyboard.TargetName="image" Storyboard.TargetProperty="(UIElement.Opacity)" />
  17.                                     </Storyboard>
  18.                                 </vsm:VisualState>
  19.                             </vsm:VisualStateGroup>
  20.                         </vsm:VisualStateManager.VisualStateGroups>
  21.                         <Canvas x:Name="HotArea">
  22.                             <Rectangle Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Fill="Green" x:Name="image" />
  23.                             <Button RenderTransformOrigin="0,0" Content="Hot Button" Cursor="Hand" x:Name="HotButton" Opacity="0.5" Canvas.Left="20" Canvas.Top="20" Width="160" Height="160" />
  24.                         </Canvas>
  25.                     </Canvas>
  26.                 </ControlTemplate>
  27.             </Setter.Value>
  28.         </Setter>
  29.     </Style>

    如果其中HotButton的范围与rectBackground重合,则鼠标移动到hotButton上虽然出发MouseEnter事件,但不会使得rectBackground改变颜色。严格的说,Mouse 必须先经过/滑动过Control中除hotButton以外的区域,然后再滑动到hotButton上时才会使rectBackground改变颜色。

 

    在这个控件中,如果要想hotButton和rectBackground重合,而又要使得鼠标移动到hotButton上rectBackground改变颜色,可以将Control的大小扩大,且将rectBackground和hotButton的位置向右下方做同等位移(位移大小10~20,是否产生效果还与鼠标移动的速度有关)。

 

  1.  public override void OnApplyTemplate()
  2.         {
  3.             base.OnApplyTemplate();
  4.             // remove events
  5.             if (_hotButton != null)
  6.             {
  7.                 _hotButton.Click -= new RoutedEventHandler(_hotButton_Click);
  8.                 _hotButton.MouseEnter -= new MouseEventHandler(_hotButton_MouseEnter);
  9.                 _hotButton.MouseLeave -= new MouseEventHandler(_hotButton_MouseLeave);
  10.             }
  11.             _hotButton = this.GetTemplateChild("HotButton"as Button;
  12.             image = this.GetTemplateChild("image"as Rectangle;
  13.             rootElemnt = this.GetTemplateChild("HotArea"as Canvas;
  14.             // add events
  15.             if (_hotButton != null)
  16.             {
  17.                 _hotButton.Click += new RoutedEventHandler(_hotButton_Click);
  18.                 _hotButton.MouseEnter += new MouseEventHandler(_hotButton_MouseEnter);
  19.                 _hotButton.MouseLeave += new MouseEventHandler(_hotButton_MouseLeave);
  20.             }
  21.             this.SetValue(Canvas.LeftProperty, -20.0);
  22.             this.SetValue(Canvas.TopProperty, -20.0);
  23.             this.Width += 40.0;
  24.             this.Height += 40.0;
  25.             rootElemnt.SetValue(Canvas.LeftProperty, 20.0);
  26.             rootElemnt.SetValue(Canvas.TopProperty, 20.0);
  27.         }        

你可能感兴趣的:(image,null,silverlight,button)