【发布时间】:2014-03-14 10:22:06
【问题描述】:
我有一个 RoundButtonTemplate 来获取音乐播放器的播放和暂停按钮等按钮。
<ControlTemplate x:Key="RoundButtonTemplate" TargetType="Button">
<Grid Background="Transparent" Margin="{StaticResource PhoneTouchTargetOverhang}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Pressed">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill)" Storyboard.TargetName="ButtonBackground">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneAccentBrush}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Normal"/>
<VisualState x:Name="Disabled">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Opacity)" Storyboard.TargetName="ButtonBackground">
<DiscreteObjectKeyFrame KeyTime="0" Value="0.5"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Opacity)" Storyboard.TargetName="ButtonBorder">
<DiscreteObjectKeyFrame KeyTime="0" Value="0.5"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Opacity)" Storyboard.TargetName="ButtonContent">
<DiscreteObjectKeyFrame KeyTime="0" Value="0.5"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="MouseOver"/>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates"/>
</VisualStateManager.VisualStateGroups>
<Border x:Name="ButtonBorder" BorderBrush="White" BorderThickness="3" Background="Transparent" CornerRadius="100">
<Ellipse x:Name="ButtonBackground" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Fill="{TemplateBinding Background}" />
</Border>
<ContentPresenter x:Name="ButtonContent" Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
如果按下按钮,按钮的背景应该变为前景色,通过 ContentPresenter 显示的图标应该变为背景色或透明。
将按钮的背景更改为前景色很简单,但是我不知道如何在ContentPresenter中更改图像的颜色?
【问题讨论】:
-
为什么要改变图片的颜色,你认为有可能吗,为什么不改变图片本身?
-
比如我想设置按钮背景为白色,图片颜色为黑色或透明。这可以通过在图像上放置 OpacityMask 来实现,但我不知道如何在 ContentPresenter 或 ContentControl 上设置它
-
你为什么不直接改变图像本身,这样会更简单
-
我应该如何更改模板中的图像?如果我在事件 Tap 或 Hold 上更改代码背景中的图像,那么我必须为每个按钮创建一个事件处理程序。
-
如果此实例的模板是隐式的,只需添加图像(用于正常和按下状态)并切换它们各自状态的可见性或不透明度。或者,如果它们是 xaml 路径向量,只需更改它们各自状态的填充。
标签: button windows-phone-8 windows-phone controltemplate