カスタム スタイルのグループ ボックスがあり、プログラムでボタンが押されたときに視覚状態を作成し、それらの視覚状態に移動しようとしています。
以下のコードは、グループボックスのスタイルを変更し、ヘッダーを青一色に変更します
また、まだ学習中のコードであるため、このコードはごちゃごちゃしているか、不十分である可能性があることに注意してください。
<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);
}
これを実行してボタンをクリックすると、スタイルはまったく変更されず、エラーも発生しません。
この時点で何が間違っているのか完全にはわかりません.カスタムコントロールの色を視覚状態でオーバーライドできますか?