使用视觉状态时,组框标头背景未更新

本文关键字:背景 更新 视觉 状态 | 更新日期: 2023-09-27 18:33:57

我有一个具有自定义样式的组框,我正在尝试为它创建视觉状态,并在点击程序上的按钮时移动到这些视觉状态。

下面的代码将分组框样式化,并将标题更改为纯蓝色

还要注意仍在学习代码,因此此代码可能很混乱或做得不好。

<Style TargetType="GroupBox">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="GroupBox">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="28" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Border Grid.Row="0"
          BorderThickness="1"
          BorderBrush="#3c4a55"
          Background="#FF0080D4">
                        <Label Foreground="White">
                            <ContentPresenter Margin="0"
                      ContentSource="Header"
                      RecognizesAccessKey="True" />
                        </Label>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup>
                                <VisualState Name="Normal"/>
                                <VisualState x:Name="Orange">
                                    <Storyboard>
                                        <ColorAnimation 
                                            Storyboard.TargetName="BackgroundColor"
                                            Storyboard.TargetProperty="Color"
                                            To="{StaticResource CVinYellow}"
                                            />
                                    </Storyboard>
                                </VisualState>
                                <VisualStateGroup.Transitions>
                                    <VisualTransition To="Orange" GeneratedDuration="00:00:01"/>
                                    <VisualTransition To="Normal" GeneratedDuration="00:00:01"/>
                                </VisualStateGroup.Transitions>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>

                    </Border>
                    <Border Grid.Row="1"
          BorderThickness="1,1,1,1"
          BorderBrush="#25A0DA">
                        <ContentPresenter Margin="1" />
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

背后的代码如下:

 private void radioButton_Checked(object sender, RoutedEventArgs e)
    {
        VisualStateManager.GoToState(testGroupBox, "Orange", true);
    }

当运行此按钮并单击按钮时,样式根本不会改变,也不会产生任何错误。

我不完全确定我现在做错了什么,你能用视觉状态覆盖自定义控件的颜色吗?

使用视觉状态时,组框标头背景未更新

在拇指捣蛋的帮助下想通了。

VisualStateManager 必须是顶部元素。不得不移动它,使其低于网格。

<Style TargetType="GroupBox">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="GroupBox">
                <Grid>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup>
                            <VisualState Name="Normal"/>
                            <VisualState x:Name="Orange">
                                <Storyboard>
                                    <ColorAnimation 
                                            Storyboard.TargetName="BorderColors"
                                            Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)"
                                            To="{StaticResource CVinYellow}"
                                            />
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="28" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Border x:Name="BorderColors"
                            Grid.Row="0"
          BorderThickness="1"
          BorderBrush="#3c4a55"
          Background="#FF0080D4">
                        <Label Foreground="White">
                            <ContentPresenter Margin="0"
                      ContentSource="Header"
                      RecognizesAccessKey="True" />
                        </Label>


                    </Border>
                    <Border Grid.Row="1"
          BorderThickness="1,1,1,1"
          BorderBrush="#25A0DA">
                        <ContentPresenter Margin="1" />
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>