我的Button
应用中的Silverlight
有自定义样式。
我使用静态样式和控制模板。在VisualStudio设计器中,我可以看到按钮上的图像,但是当我运行应用程序时,图像没有显示。
以下是样式的XAML
<Style x:Key="PlayButtonStyle" TargetType="Button">
<Setter Property="Foreground" Value="DarkGreen" />
<Setter Property="Template" Value="{StaticResource PlayButtonControlTemplate}"/>
</Style>
以下是ControlTemplate的XAML
<ControlTemplate x:Key="PlayButtonControlTemplate" TargetType="Button">
<Border x:Name="Border">
<Border.Background>
<SolidColorBrush Color="{StaticResource ControlNormalColor}" />
</Border.Background>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0:0:0.5" />
<VisualTransition GeneratedDuration="0" To="Pressed" />
</VisualStateGroup.Transitions>
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"
Storyboard.TargetName="Border">
<EasingColorKeyFrame KeyTime="0" Value="{StaticResource ControlMouseOverColor}" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"
Storyboard.TargetName="Border">
<EasingColorKeyFrame KeyTime="0" Value="{StaticResource ControlPressedColor}" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"
Storyboard.TargetName="Border">
<EasingColorKeyFrame KeyTime="0" Value="{StaticResource DisabledControlColor}" />
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)"
Storyboard.TargetName="Border">
<EasingColorKeyFrame KeyTime="0" Value="{StaticResource DisabledForegroundColor}" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid Width="55" Height="25">
<Image x:Name="DefaultImage" Source="Images/VideoPlayer/play_button.png" Opacity="1" />
<Image x:Name="HoverImage" Source="Images/VideoPlayer/play_button_hover.png" Opacity="0" />
</Grid>
</Border>
</ControlTemplate>
这是我分配的方式
<Button x:Name="playPauseButton" Content="Play" Style="{StaticResource PlayButtonStyle}" Click="playPauseButton_Click" />
答案 0 :(得分:1)
将图像构建操作设置为资源,并将代码更改为:
<Image x:Name="DefaultImage" Source="pack://application:,,,/[Your namespace];component/Images/VideoPlayer/play_button.png" Opacity="1" />
不要忘记更改为您的命名空间。
应该就够了。
修改强>:
仔细观察之后,我认为你只需要这样做:
<Image x:Name="DefaultImage" Source="/[Your namespace];component/Images/VideoPlayer/play_button.png" Opacity="1" />