之前已经提出过这个问题,但在大多数情况下,这个问题不会超过2年前,而且往往是针对
WPF的.答案可能仍然是相同的,但在这里它.我正在尝试构建一个三角形(箭头)按钮,当鼠标悬停在其上时,该按钮会改变颜色并增大尺寸.我有一个按钮工作.但是现在我需要带箭头指向不同方向的按钮.我想尽可能多地重用代码.在不使用自定义按钮控件的情况下,我无法想到完全使用相同样式的方法,所以我试图通过将鼠标重新设置为动画来重用动画.当我在我的按钮模板的visualstatemanager中将Storyboard引用为StaticResource时,它使我的按钮完全消失.为什么这不起作用?
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:System="clr-namespace:System;assembly=mscorlib" mc:Ignorable="d" x:Class="SilverlightTest.MainPage" Width="640" Height="480"> <UserControl.Resources> <Storyboard x:Key="ArrowMouSEOver"> <DoubleAnimation Duration="0:0:0.165" To="1.25" Storyboard.TargetProperty="(UiElement.RenderTransform).(ScaleTransform.ScaleX)" Storyboard.TargetName="polygon"/> <DoubleAnimation Duration="0:0:0.165" To="1.25" Storyboard.TargetProperty="(UiElement.RenderTransform).(ScaleTransform.ScaleY)" Storyboard.TargetName="polygon"/> <ColorAnimation Duration="0:0:0.165" To="#FF9BD6FF" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="polygon" d:IsOptimized="True"/> <ColorAnimation Duration="0:0:0.165" To="#FF70ACDF" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="polygon" d:IsOptimized="True"/> <ColorAnimation Duration="0:0:0.165" To="#FF7DAEFF" Storyboard.TargetProperty="(Shape.stroke).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="polygon" d:IsOptimized="True"/> <ColorAnimation Duration="0:0:0.165" To="#FF2B5CB4" Storyboard.TargetProperty="(Shape.stroke).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="polygon" d:IsOptimized="True"/> </Storyboard> <Style x:Key="LeftArrow" targettype="Button"> <Setter Property="Cursor" Value="Hand"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate targettype="Button"> <Grid x:Name="GrdRoot" RenderTransformOrigin="0.5,0.5"> <Grid.RowDeFinitions> <RowDeFinition Height="Auto"/> <RowDeFinition Height="Auto"/> </Grid.RowDeFinitions> <visualstatemanager.VisualStateGroups> <VisualStateGroup x:Name="CommonStates"> <VisualStateGroup.Transitions> <VisualTransition From="MouSEOver" GeneratedDuration="0:0:0.165"/> </VisualStateGroup.Transitions> <VisualState x:Name="normal"/> <VisualState x:Name="MouSEOver" Storyboard="{StaticResource ArrowMouSEOver}"> </VisualState> <VisualState x:Name="pressed"/> <VisualState x:Name="disabled"/> </VisualStateGroup> </visualstatemanager.VisualStateGroups> <polygon x:Name="polygon" Grid.Row="0" Margin="1" strokeThickness="{TemplateBinding BorderThickness}" HorizontalAlignment="Center" RenderTransformOrigin="0.5,0.5"> <polygon.Points> <Point X="10"/> <Point X="0" Y="5" /> <Point Y="10" X="10" /> </polygon.Points> <polygon.RenderTransform> <ScaleTransform /> </polygon.RenderTransform> <polygon.Fill> <LinearGradientBrush EndPoint="0.5,0" StartPoint="0.5,1"> <GradientStop Color="#FFA9A9A9"/> <GradientStop Color="#FFD3D3D3" Offset="1"/> </LinearGradientBrush> </polygon.Fill> <polygon.stroke> <LinearGradientBrush EndPoint="0.5,1"> <GradientStop Color="#FF696969"/> <GradientStop Color="#FF939393" Offset="1"/> </LinearGradientBrush> </polygon.stroke> </polygon> <ContentPresenter Grid.Row="1" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </UserControl.Resources> <Grid x:Name="LayoutRoot" Background="White"> <Button Style="{StaticResource LeftArrow}" HorizontalAlignment="Left" VerticalAlignment="Top"> </Button> </Grid>
解决方法
这听起来像你应该介绍自己的Button类.
我会这样做:
我会这样做:
<my:GlowingArrowButton ArrowDirection="Left"/>
你的generic.xaml:
<Style targettype="my:GlowingArrowButton"> <Setter Property="Cursor" Value="Hand"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate targettype="my:GlowingArrowButton"> <Grid x:Name="GrdRoot" RenderTransformOrigin="0.5,0.5"> <Grid.RowDeFinitions> <RowDeFinition Height="Auto"/> <RowDeFinition Height="Auto"/> </Grid.RowDeFinitions> <visualstatemanager.VisualStateGroups> <VisualStateGroup x:Name="CommonStates"> <VisualStateGroup.Transitions> <VisualTransition From="MouSEOver" GeneratedDuration="0:0:0.165"/> </VisualStateGroup.Transitions> <VisualState x:Name="normal"/> <VisualState x:Name="MouSEOver" <Storyboard> <DoubleAnimation Duration="0:0:0.165" To="1.25" Storyboard.TargetProperty="(UiElement.RenderTransform).(ScaleTransform.ScaleX)" Storyboard.TargetName="polygon"/> <DoubleAnimation Duration="0:0:0.165" To="1.25" Storyboard.TargetProperty="(UiElement.RenderTransform).(ScaleTransform.ScaleY)" Storyboard.TargetName="polygon"/> <ColorAnimation Duration="0:0:0.165" To="#FF9BD6FF" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="polygon" d:IsOptimized="True"/> <ColorAnimation Duration="0:0:0.165" To="#FF70ACDF" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="polygon" d:IsOptimized="True"/> <ColorAnimation Duration="0:0:0.165" To="#FF7DAEFF" Storyboard.TargetProperty="(Shape.stroke).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="polygon" d:IsOptimized="True"/> <ColorAnimation Duration="0:0:0.165" To="#FF2B5CB4" Storyboard.TargetProperty="(Shape.stroke).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="polygon" d:IsOptimized="True"/> </Storyboard> </VisualState> <VisualState x:Name="pressed"/> <VisualState x:Name="disabled"/> </VisualStateGroup> </visualstatemanager.VisualStateGroups> <LayoutTransformer LayoutTransform="{Binding Path=ArrowDirection,RelativeSource={RelativeSource TemplatedParent},Converter={StaticResource RotationTranslator_ToBeImplemented}}" Grid.Row="0" HorizontalAlignment="Center"> <polygon x:Name="polygon" Margin="1" strokeThickness="{TemplateBinding BorderThickness}" RenderTransformOrigin="0.5,0.5"> <polygon.Points> <Point X="10"/> <Point X="0" Y="5" /> <Point Y="10" X="10" /> </polygon.Points> <polygon.RenderTransform> <ScaleTransform /> </polygon.RenderTransform> <polygon.Fill> <LinearGradientBrush EndPoint="0.5,1"> <GradientStop Color="#FFA9A9A9"/> <GradientStop Color="#FFD3D3D3" Offset="1"/> </LinearGradientBrush> </polygon.Fill> <polygon.stroke> <LinearGradientBrush EndPoint="0.5,1"> <GradientStop Color="#FF696969"/> <GradientStop Color="#FF939393" Offset="1"/> </LinearGradientBrush> </polygon.stroke> </polygon> </LayoutTransformer> <ContentPresenter Grid.Row="1" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>
和代码:
public class GlowingArrowButton : ButtonBase { public GlowingArrowButton() { DefaultStyleKey = typeof (GlowingArrowButton); } public ArrowDirection ArrowDirection { get { return (ArrowDirection) GetValue( ArrowDirectionProperty ); } set { SetValue( ArrowDirectionProperty,value ); } } public static readonly DependencyProperty ArrowDirectionProperty = DependencyProperty.Register( "ArrowDirection",typeof( ArrowDirection ),typeof( GlowingArrowButton ),new PropertyMetadata( default( ArrowDirection ) ) ); } public enum ArrowDirection { Left,Up,Right,Down }
[编辑]
书面但未经测试:
public class RotationTranslator : IValueConverter { public object Convert(object value,Type targettype,object parameter,CultureInfo culture) { var arrowDirection = (ArrowDirection) value; switch (arrowDirection) { case ArrowDirection.Left: return new RotateTransform{ Angle = 0 }; case ArrowDirection.Up: return new RotateTransform { Angle = 90 }; case ArrowDirection.Right: return new RotateTransform { Angle = 180 }; case ArrowDirection.Down: return new RotateTransform { Angle = -90 }; } throw new InvalidOperationException(); } public object ConvertBack(object value,CultureInfo culture){throw new NotSupportedException();} }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。