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

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

     

 

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.         }        
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值