我专门在 VS Blend 2017 设计器中设计了一个窗口,而没有对 XAML 代码进行任何更改。我创建了一个 StateGroup 和一个 State 并记录了一个 Button 上的更改。尝试从代码隐藏应用状态时,没有任何反应。
我也看过同一问题的主题,但不幸的是没有一个对我有用!
代码隐藏:
VisualStateManager.GoToState(this, "State1", false);
XAML:
<Window x:Class="Wpf_2017.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Wpf_2017"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="MyStates">
<VisualState x:Name="State1">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="button">
<EasingColorKeyFrame KeyTime="0" Value="#FFEA8686"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="button">
<EasingColorKeyFrame KeyTime="0" Value="#FFFF4848"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Button x:Name="button" Content="Button" Margin="208.306,116.798,218.093,0" VerticalAlignment="Top" Height="26.065" Click="button_Click"/>
</Grid>
</Window>
谢谢你的帮助!
视觉状态应ControlTemplate
在控件的 中定义:
<Window x:Class="Wpf_2017.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp2"
mc:Ignorable="d"
Title="Window10" Height="300" Width="300">
<Window.Template>
<ControlTemplate TargetType="{x:Type Window}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="MyStates">
<VisualState x:Name="State1">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="button">
<EasingColorKeyFrame KeyTime="0" Value="#FFEA8686"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="button">
<EasingColorKeyFrame KeyTime="0" Value="#FFFF4848"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<AdornerDecorator>
<StackPanel>
<Button x:Name="button" Content="Button" Margin="208.306,116.798,218.093,0" VerticalAlignment="Top" Height="26.065" Click="button_Click"/>
</StackPanel>
</AdornerDecorator>
</Border>
</ControlTemplate>
</Window.Template>
</Window>
那就是VisualStateManager
寻找他们的地方。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句