如何在 Windows Phone 中更改 RadioButton 的按下状态
Posted
技术标签:
【中文标题】如何在 Windows Phone 中更改 RadioButton 的按下状态【英文标题】:How to Change the RadioButton Pressed State in Windows Phone 【发布时间】:2013-09-21 07:02:19 【问题描述】:我在我的应用程序中使用两个 RadioButtons 作为选择选项,而不是在按下 RadioButton 时显示默认的强调色,我想拥有自己的颜色“#FF1BA1E2”。这是出于自定义主题目的。我该如何更改此状态以解决此问题?到目前为止,我所拥有的如下,它实现了 RadioButtons 但不是自定义颜色功能。
MainPage.xaml.cs
<RadioButton x:Name="ascendingSortRadioButton" Content="Binding Path=LocalizedResources.SettingsPage_PictureSortOrder_Ascending, Source=StaticResource LocalizedStrings"
GroupName="pictureSort" Checked="radioButton_Check">
<RadioButton.ContentTemplate>
<DataTemplate>
<TextBlock TextWrapping="Wrap" Text="Binding"/>
</DataTemplate>
</RadioButton.ContentTemplate>
</RadioButton>
<RadioButton x:Name="descendingSortRadioButton" Content="Binding Path=LocalizedResources.SettingsPage_PictureSortOrder_Descending, Source=StaticResource LocalizedStrings"
GroupName="pictureSort" Checked="radioButton_Check">
<RadioButton.ContentTemplate>
<DataTemplate>
<TextBlock TextWrapping="Wrap" Text="Binding"/>
</DataTemplate>
</RadioButton.ContentTemplate>
</RadioButton>
我需要自定义样式吗?如果是这样,我将如何做到这一点?
【问题讨论】:
【参考方案1】:要编辑 RadioButton 的按下状态,您需要编辑他的样式。要编辑控件的样式,请打开大纲窗口 > 在树中找到您的控件 > 单击右键 > 编辑模板 > 编辑副本。它将生成如下样式:
<Style x:Key="RadioButtonStyle1" TargetType="RadioButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="RadioButton">
<Grid Background="Transparent">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver"/>
<VisualState x:Name="Pressed">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="CheckBackground">
<DiscreteObjectKeyFrame KeyTime="0" Value="StaticResource PhoneRadioCheckBoxPressedBrush"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="CheckMark">
<DiscreteObjectKeyFrame KeyTime="0" Value="StaticResource PhoneButtonBasePressedForegroundBrush"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Stroke" Storyboard.TargetName="CheckBackground">
<DiscreteObjectKeyFrame KeyTime="0" Value="StaticResource PhoneDisabledBrush"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="CheckMark">
<DiscreteObjectKeyFrame KeyTime="0" Value="StaticResource PhoneDisabledBrush"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
<DiscreteObjectKeyFrame KeyTime="0" Value="StaticResource PhoneDisabledBrush"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="CheckStates">
<VisualState x:Name="Checked">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="CheckMark">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Unchecked"/>
<VisualState x:Name="Indeterminate"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid Margin="StaticResource PhoneTouchTargetLargeOverhang">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="32"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Ellipse x:Name="CheckBackground" Fill="TemplateBinding Background" HorizontalAlignment="Left" Height="32" IsHitTestVisible="False" Stroke="TemplateBinding BorderBrush" StrokeThickness="StaticResource PhoneStrokeThickness" VerticalAlignment="Center" Width="32"/>
<Ellipse x:Name="CheckMark" Fill="StaticResource PhoneRadioCheckBoxCheckBrush" HorizontalAlignment="Center" Height="16" IsHitTestVisible="False" Visibility="Collapsed" VerticalAlignment="Center" Width="16"/>
<ContentControl x:Name="ContentContainer" ContentTemplate="TemplateBinding ContentTemplate" Content="TemplateBinding Content" Grid.Column="1" Foreground="TemplateBinding Foreground" FontSize="TemplateBinding FontSize" FontFamily="TemplateBinding FontFamily" HorizontalContentAlignment="TemplateBinding HorizontalContentAlignment" Margin="12,0,0,0" Padding="TemplateBinding Padding" VerticalContentAlignment="TemplateBinding VerticalContentAlignment"/>
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
你要修改的是RadioButtonStyle1视觉状态的Pressed State中定义的颜色
【讨论】:
是否可以只保留RadioButtonStyle1
并删除其他所有内容?还是RadioButtonStyle1
依赖于其他样式?
编辑了我的回复以删除其他样式(并删除 basedOn)。这将起作用,因为无论如何属性都将从基于 RadioButton 的样式继承以上是关于如何在 Windows Phone 中更改 RadioButton 的按下状态的主要内容,如果未能解决你的问题,请参考以下文章
如何在单击并再次在 Windows Phone 中释放时更改按钮图像
如何更改 windows phone 应用程序中按钮的背景颜色?
如何在 windows phone 8.1 应用程序中使用 MenuFlyout 更改动态生成按钮的内容
如何以编程方式更改 Windows Phone 中设置器值中的图像