我想使用图像在Silverlight中创建一个具有基本效果的按钮,以使按钮看起来可点击。我创建了一个.png图标并编写了一些代码,以便在故事板中鼠标悬停时使这个图像略大一些。这是我第一次尝试做这样的事情,所以我假设我已经遗漏了对其中一个静态资源的简单调用。我错过了什么? :
<Style TargetType="Button"
x:Key="styleA">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid x:Name="RootElement"
Background="Transparent">
<Grid.Resources>
<Storyboard x:Key="MouseOver State">
<DoubleAnimation Storyboard.TargetName="buttonScale"
Storyboard.TargetProperty="ScaleX"
To="1.2"
Duration="00:00:00.25"/>
<DoubleAnimation Storyboard.TargetName="buttonScale"
Storyboard.TargetProperty="ScaleY"
To="1.2"
Duration="00:00:00.25" />
</Storyboard>
<Storyboard x:Key="Normal State">
<DoubleAnimation Storyboard.TargetName="buttonScale"
Storyboard.TargetProperty="ScaleX"
To="1"
Duration="00:00:00.25" />
<DoubleAnimation Storyboard.TargetName="buttonScale"
Storyboard.TargetProperty="ScaleY"
To="1"
Duration="00:00:00.25" />
</Storyboard>
<Storyboard x:Key="Pressed State">
<DoubleAnimation Storyboard.TargetName="buttonScale"
Storyboard.TargetProperty="ScaleX"
To="1.4"
Duration="00:00:00.25" />
<DoubleAnimation Storyboard.TargetName="buttonScale"
Storyboard.TargetProperty="ScaleY"
To="1.4"
Duration="00:00:00.25" />
</Storyboard>
</Grid.Resources>
<ContentPresenter Content="{TemplateBinding Content}"
RenderTransformOrigin="0.5,0.5">
<ContentPresenter.RenderTransform>
<ScaleTransform x:Name="buttonScale" />
</ContentPresenter.RenderTransform>
</ContentPresenter>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
你可以看到一个简单的故事板。现在,当我实现我的按钮时,没有任何反应。我究竟做错了什么?这是我创建按钮的代码:
<StackPanel>
<Button Width="50"
x:Name="Test"
Style="{StaticResource styleA}">
<StackPanel Orientation="Vertical">
<Image Source="test.png" />
<TextBlock Text="Please get bigger" />
</StackPanel>
</Button>
</StackPanel>
答案 0 :(得分:0)
你看过Button Styles and Templates了吗?您需要将这些故事板放在VisualStateManager.VisualStateGroups
内,如下所示:
<ControlTemplate TargetType="Button">
<Grid x:Name="RootElement" Background="Transparent">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="MouseOver">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="buttonScale" Storyboard.TargetProperty="ScaleX" To="1.2" Duration="00:00:00.25"/>
<DoubleAnimation Storyboard.TargetName="buttonScale" Storyboard.TargetProperty="ScaleY" To="1.2" Duration="00:00:00.25" />
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="buttonScale" Storyboard.TargetProperty="ScaleX" To="1.4" Duration="00:00:00.25" />
<DoubleAnimation Storyboard.TargetName="buttonScale" Storyboard.TargetProperty="ScaleY" To="1.4" Duration="00:00:00.25" />
</Storyboard>
</VisualState>
<VisualState x:Name="Normal"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ContentPresenter Content="{TemplateBinding Content}" RenderTransformOrigin="0.5,0.5">
<ContentPresenter.RenderTransform>
<ScaleTransform x:Name="buttonScale" />
</ContentPresenter.RenderTransform>
</ContentPresenter>
</Grid>
</ControlTemplate>