我有一个按钮控件,它有 2 种视觉状态;说“简单”和“复杂”;前者是默认值。最初控件是隐藏的。
当我通过VisualStateManager将视觉状态设置为“复杂”时,我得到false
了true
是控件是否至少显示一次;如果是这样,则尊重视觉状态更改,否则它总是被忽略,直到它显示一次。
如何为尚不可见的控件设置视觉状态?
<Grid x:Name="LayoutRoot"
Background="LightBlue">
<Grid.Resources>
<Style x:Key="MyButton"
TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="MyStates">
<VisualState x:Name="Simple" />
<VisualState x:Name="Complex">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ellipse"
Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
<DiscreteObjectKeyFrame KeyTime="10"
Value="#00000000" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Ellipse x:Name="ellipse"
Fill="#FF0E0EF5"
Stroke="Black" />
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<Button x:Name="btnStuff"
Content="Click Me!"
Visibility="Collapsed"
Style="{StaticResource MyButton}" />
<StackPanel>
<Button x:Name="btnShow"
Content="Show"
Click="btnShow_Click" />
<Button x:Name="btnVisual"
Content="Visual Change"
Click="btnVisual_Click" />
</StackPanel>
</Grid>
后面的代码:
bool fOriginalState = true;
private void btnShow_Click(object sender, RoutedEventArgs e)
{
btnStuff.Visibility = btnStuff.Visibility == Windows.UI.Xaml.Visibility.Visible ? Windows.UI.Xaml.Visibility.Collapsed : Windows.UI.Xaml.Visibility.Visible;
}
private void btnVisual_Click(object sender, RoutedEventArgs e)
{
string visualState = fOriginalState ? "Complex" : "Simple";
fOriginalState = !fOriginalState;
bool fSet = VisualStateManager.GoToState(btnStuff, visualState, false);
System.Diagnostics.Debug.WriteLine(fSet.ToString());
}