我在XAML中制作单选按钮样式,如果按下状态,我想更改Content Presenter(文本)的颜色。我希望有可能。
这是我的代码:
<Style x:Key="NavButtonsStyle1" TargetType="RadioButton">
<Setter Property="Background" Value="#FF448DCA"/>
<Setter Property="Foreground" Value="#FF000000"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Top"/>
<Setter Property="Padding" Value="4,1,0,0"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush">
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFA3AEB9" Offset="0"/>
<GradientStop Color="#FF8399A9" Offset="0.375"/>
<GradientStop Color="#FF718597" Offset="0.375"/>
<GradientStop Color="#FF617584" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="RadioButton">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="55"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BackgroundOverlay"/>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BoxMiddleBackground"/>
<ColorAnimation Duration="0" To="#7FFFFFFF" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[3].(GradientStop.Color)" Storyboard.TargetName="BoxMiddle"/>
<ColorAnimation Duration="0" To="#CCFFFFFF" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[2].(GradientStop.Color)" Storyboard.TargetName="BoxMiddle"/>
<ColorAnimation Duration="0" To="#F2FFFFFF" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="BoxMiddle"/>
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed">
<Storyboard>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BackgroundOverlay"/>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BoxMiddleBackground"/>
<ColorAnimation Duration="0" To="#6BFFFFFF" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[3].(GradientStop.Color)" Storyboard.TargetName="BoxMiddle"/>
<ColorAnimation Duration="0" To="#C6FFFFFF" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[2].(GradientStop.Color)" Storyboard.TargetName="BoxMiddle"/>
<ColorAnimation Duration="0" To="#EAFFFFFF" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="BoxMiddle"/>
<ColorAnimation Duration="0" To="#F4FFFFFF" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="BoxMiddle"/>
<ColorAnimation Duration="0" To="#FF6DBDD1" Storyboard.TargetProperty="(Shape.Stroke).(GradientBrush.GradientStops)[3].(GradientStop.Color)" Storyboard.TargetName="BoxMiddle"/>
<ColorAnimation Duration="0" To="#FF6DBDD1" Storyboard.TargetProperty="(Shape.Stroke).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="BoxMiddle"/>
<ColorAnimation Duration="0" To="#FF6DBDD1" Storyboard.TargetProperty="(Shape.Stroke).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="BoxMiddle"/>
<ColorAnimation Duration="0" To="#FF6DBDD1" Storyboard.TargetProperty="(Shape.Stroke).(GradientBrush.GradientStops)[2].(GradientStop.Color)" Storyboard.TargetName="BoxMiddle"/>
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled">
<Storyboard>
<DoubleAnimation Duration="0" To=".55" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="contentPresenter"/>
<DoubleAnimation Duration="0" To="0.55" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="DisabledVisualElement"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="CheckStates">
<VisualState x:Name="Checked">
<Storyboard>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="CheckIcon"/>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="Selected_Copy" d:IsOptimized="True"/>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="contentPresenter_Copy1" d:IsOptimized="True"/>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="image" d:IsOptimized="True"/>
</Storyboard>
</VisualState>
<VisualState x:Name="Unchecked">
<Storyboard>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="Unselected_Copy" d:IsOptimized="True"/>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="contentPresenter_Copy" d:IsOptimized="True"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused">
<Storyboard>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="ContentFocusVisualElement"/>
</Storyboard>
</VisualState>
<VisualState x:Name="Unfocused"/>
</VisualStateGroup>
<VisualStateGroup x:Name="ValidationStates">
<VisualState x:Name="Valid"/>
<VisualState x:Name="InvalidUnfocused">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="ValidationErrorElement">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="InvalidFocused">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="ValidationErrorElement">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="IsOpen" Storyboard.TargetName="validationTooltip">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<System:Boolean>True</System:Boolean>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid HorizontalAlignment="Right" VerticalAlignment="Center">
<Ellipse x:Name="Background" Fill="#FFFFFFFF" Height="14" Margin="1" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="{TemplateBinding BorderThickness}" Width="14"/>
<Ellipse x:Name="BackgroundOverlay" Fill="#FFC4DBEE" Height="14" Margin="1" Opacity="0" Stroke="#00000000" StrokeThickness="1" Width="14"/>
<Ellipse x:Name="BoxMiddleBackground" Fill="{TemplateBinding Background}" Height="10" Stroke="#00000000" StrokeThickness="1" Width="10"/>
<Ellipse x:Name="BoxMiddle" Height="10" StrokeThickness="1" Width="10">
<Ellipse.Fill>
<LinearGradientBrush EndPoint="0.64,0.88" StartPoint="0.62,0.15">
<GradientStop Color="#FFFFFFFF" Offset="0.013"/>
<GradientStop Color="#F9FFFFFF" Offset="0.375"/>
<GradientStop Color="#EAFFFFFF" Offset="0.603"/>
<GradientStop Color="#D8FFFFFF" Offset="1"/>
</LinearGradientBrush>
</Ellipse.Fill>
<Ellipse.Stroke>
<LinearGradientBrush EndPoint=".5,1" StartPoint=".5,0">
<GradientStop Color="#FFFFFFFF" Offset="1"/>
<GradientStop Color="#FFFFFFFF" Offset="0"/>
<GradientStop Color="#FFFFFFFF" Offset="0.375"/>
<GradientStop Color="#FFFFFFFF" Offset="0.375"/>
</LinearGradientBrush>
</Ellipse.Stroke>
</Ellipse>
<Ellipse x:Name="BoxMiddleLine" Height="10" Opacity=".2" Stroke="#FF333333" StrokeThickness="1" Width="10"/>
<Ellipse x:Name="CheckIcon" Fill="#FF333333" Height="4" Opacity="0" Width="4"/>
<Ellipse x:Name="DisabledVisualElement" Fill="#FFFFFFFF" Height="14" Opacity="0" Width="14"/>
<Ellipse x:Name="ContentFocusVisualElement" Height="16" IsHitTestVisible="false" Opacity="0" Stroke="#FF6DBDD1" StrokeThickness="1" Width="16"/>
<Grid x:Name="ValidationErrorElement" ToolTipService.PlacementTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" Visibility="Collapsed">
<ToolTipService.ToolTip>
<ToolTip x:Name="validationTooltip" DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}" Placement="Right" PlacementTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" Template="{StaticResource ValidationToolTipTemplate}">
<ToolTip.Triggers>
<EventTrigger RoutedEvent="Canvas.Loaded">
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="IsHitTestVisible" Storyboard.TargetName="validationTooltip">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<System:Boolean>true</System:Boolean>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ToolTip.Triggers>
</ToolTip>
</ToolTipService.ToolTip>
<Ellipse Height="14" Stroke="#FFDB000C" StrokeThickness="1" Width="14"/>
<Ellipse Fill="#FFDB000C" HorizontalAlignment="Right" Height="4" Margin="0,-2,-1,0" VerticalAlignment="Top" Width="4"/>
<Ellipse Fill="Transparent" HorizontalAlignment="Right" Height="10" Margin="0,-5,-4,0" VerticalAlignment="Top" Width="10"/>
</Grid>
</Grid>
<Path x:Name="Selected" Grid.ColumnSpan="2" Data="M0,0 L170,0 L170,0.12136253 L170.08366,0.083332568 L178.269,50 L170,99.778702 L170,100 L0,100 z" Stretch="Fill" UseLayoutRounding="False" Stroke="Snow" StrokeThickness="2" d:IsHidden="True">
<Path.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF1F1F1F" Offset="1"/>
<GradientStop Color="White" Offset="0.05"/>
<GradientStop Color="#FF9B9B9B" Offset="0.947"/>
<GradientStop Color="#FF818181"/>
</LinearGradientBrush>
</Path.Fill>
</Path>
<Path x:Name="Unselected" Grid.ColumnSpan="2" Data="M0,0 L170,0 L170,0.12136253 L170.08366,0.083332568 L178.269,50 L170,99.778702 L170,100 L0,100 z" Stretch="Fill" UseLayoutRounding="False" Stroke="Snow" StrokeThickness="2">
<Path.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF1F1F1F" Offset="1"/>
<GradientStop Color="#FFCAC9C9" Offset="0.05"/>
<GradientStop Color="#FF7C7A7A" Offset="0.947"/>
<GradientStop Color="#FF818181"/>
</LinearGradientBrush>
</Path.Fill>
</Path>
<ContentPresenter x:Name="contentPresenter" Content="{TemplateBinding Content}" Grid.Column="1" HorizontalAlignment="Center" Margin="-20,0,0,0" VerticalAlignment="Center"/>
<Image HorizontalAlignment="Right" VerticalAlignment="Center" Margin="10,10,0,10" Source="Edit.png"/>
<Path x:Name="Unselected_Copy" Grid.ColumnSpan="2" Data="M0,0 L170,0 L170,0.12136253 L170.08366,0.083332568 L178.269,50 L170,99.778702 L170,100 L0,100 z" Stretch="Fill" UseLayoutRounding="False" Stroke="Snow" StrokeThickness="2" Opacity="0">
<Path.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF1F1F1F" Offset="1"/>
<GradientStop Color="#FFCAC9C9" Offset="0.05"/>
<GradientStop Color="#FF7C7A7A" Offset="0.947"/>
<GradientStop Color="#FF818181"/>
</LinearGradientBrush>
</Path.Fill>
</Path>
<ContentPresenter x:Name="contentPresenter_Copy" Content="{TemplateBinding Content}" Grid.Column="1" HorizontalAlignment="Center" Margin="-20,0,0,0" VerticalAlignment="Center" Opacity="0"/>
<Path x:Name="Selected_Copy" Grid.ColumnSpan="2" Data="M0,0 L170,0 L170,0.12136253 L170.08366,0.083332568 L178.269,50 L170,99.778702 L170,100 L0,100 z" Stretch="Fill" UseLayoutRounding="False" Stroke="Snow" StrokeThickness="2" Opacity="0" d:IsHidden="True">
<Path.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF1F1F1F" Offset="1"/>
<GradientStop Color="White" Offset="0.05"/>
<GradientStop Color="#FF9B9B9B" Offset="0.947"/>
<GradientStop Color="#FF818181"/>
</LinearGradientBrush>
</Path.Fill>
</Path>
<ContentPresenter x:Name="contentPresenter_Copy1" Content="{TemplateBinding Content}" Grid.Column="1" HorizontalAlignment="Center" Margin="-20,0,0,0" VerticalAlignment="Center" Opacity="0"/>
<Image x:Name="image" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="10,10,0,10" Source="Edit.png" Opacity="0"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
答案 0 :(得分:0)
您可以将ContentPresenter更改为ContentControl,或者将ContentPresenter替换为TextBlock,并将其Text TemplateBinding设置为Content。