3

我有一个按钮,我修改了它的模板,所以当按钮被点击和鼠标悬停时它显示不同的背景颜色(它变成绿色而不是浅蓝色)。

这本身就很好用,但我需要根据某些状态更改背景颜色,所以我需要更改代码中的背景。

我的问题是当我更改代码中的背景颜色时,它会正确更改,但不再更改单击或鼠标悬停事件的背景。

我假设我以某种方式覆盖了代码中的背景颜色,因此涵盖了我在模板中创建的内容。如何解决此问题,以便我可以通过代码更改背景但仍然可以单击和鼠标悬停?

这是我的 XAML 代码:

<Style x:Key="NodeButtonStyle" TargetType="{x:Type Button}">
            <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
            <Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/>
            <Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="Padding" Value="1"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Microsoft_Windows_Themes:ButtonChrome x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}"  RenderDefaulted="{TemplateBinding IsDefaulted}" SnapsToDevicePixels="true" OverridesDefaultStyle="true">
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Microsoft_Windows_Themes:ButtonChrome>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsKeyboardFocused" Value="true">
                                <Setter Property="Background">
                                    <Setter.Value>
                                        <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                                            <GradientStop Color="#FFCFF9C4" Offset="0"/>
                                            <GradientStop Color="#FF249505" Offset="1"/>
                                            <GradientStop Color="#FF58D835" Offset="0.5"/>
                                        </LinearGradientBrush>
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="#ADADAD"/>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Background">
                                    <Setter.Value>
                                        <LinearGradientBrush EndPoint="0,1" StartPoint="0,0" Opacity="0.5">
                                            <GradientStop Color="#FFCFF9C4" Offset="0"/>
                                            <GradientStop Color="#FF249505" Offset="1"/>
                                            <GradientStop Color="#FF58D835" Offset="0.5"/>
                                        </LinearGradientBrush>
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

在代码中,我只是做 button1.Background = newBrush;

我试图寻找类似的问题,但我找不到任何..任何帮助将不胜感激!我正在使用混合 4 .net 3.5 和 C#。

4

2 回答 2

1

我相信您遇到了DependecyProperty 值优先级问题。在代码中设置值会覆盖在您的样式中设置的值。不要直接在后面的代码中设置属性,而是尝试更改按钮上的样式。另一种解决方案是将背景绑定到 DataContext 上的属性并将其设置在那里而不是您的样式。

于 2012-06-20T03:01:38.263 回答
0

丹是对的,这就是你如何绕过它

                  <Trigger Property="IsMouseOver" Value="True">
                            <Trigger.EnterActions>
                                <BeginStoryboard Name="fred">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <LinearGradientBrush EndPoint="0,1" StartPoint="0,0" Opacity="0.5">
                                                        <GradientStop Color="#FFCFF9C4" Offset="0"/>
                                                        <GradientStop Color="#FF249505" Offset="1"/>
                                                        <GradientStop Color="#FF58D835" Offset="0.5"/>
                                                    </LinearGradientBrush>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.EnterActions>
                            <Trigger.ExitActions>
                                <RemoveStoryboard BeginStoryboardName="fred" />
                            </Trigger.ExitActions>
                        </Trigger>

请注意,在场景中我们使用了 ObjectAnimation 而不是 ColorAnimation,因此我们不需要对背景最初设置的画笔类型做出任何假设。

于 2012-06-20T03:32:29.617 回答