在Silverlight 2 beta2中,开发一个自定义控件ActionSpotbutton,该控件包含一个背景矩形rectBackground和一个按钮hotbutton。想当用户鼠标移动到button上时改变背景矩形的颜色。
generic.xaml文件内容如下:
<Style targettype="local:ActionSpotbutton"> <Setter Property="IsEnabled" Value="True" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate targettype="local:ActionSpotbutton"> <Canvas x:name="RootElement" Background="WhiteSmoke"> <vsm:visualstatemanager.VisualStateGroups> <vsm:VisualStateGroup x:name="CommonStates"> <vsm:VisualState x:name="normal" > <Storyboard> <DoubleAnimation BeginTime="00:00:00" From="0" To="0.5" Duration="0" Storyboard.Targetname="image" Storyboard.TargetProperty="(UIElement.Opacity)" /> </Storyboard> </vsm:VisualState> <vsm:VisualState x:name="MouSEOver1"> <Storyboard> <DoubleAnimation BeginTime="00:00:00" From="0" To="1" Duration="0" Storyboard.Targetname="image" Storyboard.TargetProperty="(UIElement.Opacity)" /> </Storyboard> </vsm:VisualState> </vsm:VisualStateGroup> </vsm:visualstatemanager.VisualStateGroups> <Canvas x:name="HotArea"> <Rectangle WIDth="{TemplateBinding WIDth}" Height="{TemplateBinding Height}" Fill="Green" x:name="image" /> <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" /> </Canvas> </Canvas> </ControlTemplate> </Setter.Value> </Setter> </Style>如果其中Hotbutton的范围与rectBackground重合,则鼠标移动到hotbutton上虽然出发MouseEnter事件,但不会使得rectBackground改变颜色。严格的说,Mouse 必须先经过/滑动过Control中除hotbutton以外的区域,然后再滑动到hotbutton上时才会使rectBackground改变颜色。
在这个控件中,如果要想hotbutton和rectBackground重合,而又要使得鼠标移动到hotbutton上rectBackground改变颜色,可以将Control的大小扩大,且将rectBackground和hotbutton的位置向右下方做同等位移(位移大小10~20,是否产生效果还与鼠标移动的速度有关)。
public overrIDe voID OnApplyTemplate() { base.OnApplyTemplate(); // remove events if (_hotbutton != null) { _hotbutton.Click -= new RoutedEventHandler(_hotbutton_Click); _hotbutton.MouseEnter -= new MouseEventHandler(_hotbutton_MouseEnter); _hotbutton.MouseLeave -= new MouseEventHandler(_hotbutton_MouseLeave); } _hotbutton = this.GetTemplateChild("Hotbutton") as button; image = this.GetTemplateChild("image") as Rectangle; rootElemnt = this.GetTemplateChild("HotArea") as Canvas; // add events if (_hotbutton != null) { _hotbutton.Click += new RoutedEventHandler(_hotbutton_Click); _hotbutton.MouseEnter += new MouseEventHandler(_hotbutton_MouseEnter); _hotbutton.MouseLeave += new MouseEventHandler(_hotbutton_MouseLeave); } this.SetValue(Canvas.leftProperty, -20.0); this.SetValue(Canvas.topProperty, -20.0); this.WIDth += 40.0; this.Height += 40.0; rootElemnt.SetValue(Canvas.leftProperty, 20.0); rootElemnt.SetValue(Canvas.topProperty, 20.0); } 总结
以上是内存溢出为你收集整理的Silverlight 2b2 中自定义控件中子控件MouseEnter事件改变VisualState的问题全部内容,希望文章能够帮你解决Silverlight 2b2 中自定义控件中子控件MouseEnter事件改变VisualState的问题所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)