2

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

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

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

<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);
    }

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

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

4

1 回答 1

2

在拇指猴的帮助下想通了。

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>
于 2014-12-11T23:00:19.873 回答