以编程方式覆盖背景样式

时间:2012-04-20 15:29:55

标签: wpf wpf-controls

我有一系列应用了样式的ToggleButtons。我希望能够在选中按钮时更改背景颜色。每个ToggleButton都分配给相同的Checked事件,该事件具有以下代码。

Private Sub togAnswer_Checked(ByVal sender as Object, ByVal e as System.Windows.RoutedEventArgs)
    'togAnswer was clicked....
    Dim togC as ToggleButton
    Dim togS as String = TryCast(sender, ToggleButton).Name.ToString()
    'Cycle through all answers and set style
    Dim bc = New BrushConverter()

    For x = 1 to 3
        togC = DirectCast(FindName("togAnswer" & x), ToggleButton) 

        If togS = "togAnswer" & x.ToString Then
            'Set ToggleButton to selected colour
            togC.Background = DirectCast(bc.ConvertFrom("#AAFF8020"), Brush)
        Else    
            'Set other ToggleButtons to unselected colour
            togC.Background = DirectCast(bc.ConvertFrom("#AA000000"), Brush)
        End If
    Next
End Sub

代码正确识别源ToggleButton但不改变颜色。

分配给ToggleButtons的样式是:

<Style x:Key="GlassToggleButton" TargetType="{x:Type ToggleButton}">
    <Setter Property="FontSize" Value="24" />
    <Setter Property="Foreground" Value="White" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ToggleButton}">
                <Border x:Name="ToggleBorder" 
                    CornerRadius="0 15 0 15" 
                    Background="#AA000000"  
                    BorderBrush="#99FFFFFF"
                    RenderTransformOrigin="0.5,0.5">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="1.7*"/>
                        </Grid.RowDefinitions>
                        <Border Grid.Row="0" CornerRadius="0,14,0,0">
                            <Border.Background>
                                <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                                    <GradientStop Color="#02FFFFFF" Offset="0"/>
                                    <GradientStop Color="#55FFFFFF" Offset="0.5"/>
                                    <GradientStop Color="#11FFFFFF" Offset="1"/>
                                </LinearGradientBrush>
                            </Border.Background>
                        </Border>
                        <ContentPresenter x:Name="ToggleContentPresenter"
                            VerticalAlignment="Center"
                            HorizontalAlignment="Center"
                            Grid.RowSpan="2" />
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter Property="RenderTransform" TargetName="ToggleBorder">
                            <Setter.Value>
                                <TransformGroup>
                                    <ScaleTransform ScaleX="0.95" ScaleY="0.95"/>
                                </TransformGroup>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="Background" Value="#AAFF8020" />
                    </Trigger>
                    <Trigger Property="IsChecked" Value="True">
                        <Setter Property="Background" Value="#AAFF8020" />
                    </Trigger>
                    <Trigger Property="IsChecked" Value="False">
                        <Setter Property="Background" Value="#AA000000" />
                    </Trigger>
                    <Trigger Property="IsChecked" Value="{x:Null}">
                        <Setter Property="Background" Value="#AA000000" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

1 个答案:

答案 0 :(得分:1)

由于您在模板中明确设置了边框的背景颜色,因此覆盖Background的触发器将无效(您的模板将忽略它)。你需要做的是重构你的风格以尊重控件自己的Background

不要在Background="#AA000000"上设置Border,而是设置Background="{TemplateBinding Background}" - this kind of binding only works within a ControlTemplate . Then, add another setter in your style that sets背景to#AA000000`。然后你应该发现你的触发器有效!