Ray*_*ink 4 c# xaml visual-studio-2010 visualstatemanager windows-runtime
我的代码更改属性不起作用,我完全不知道什么是错的,但也许你这样做.
这是我的代码的简化示例,它重现了错误.这是我的Xaml代码:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Button x:Name="MyButton"
Height="100"
Width="300"
Content="Click"
FontSize="40"
FontWeight="Bold"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Background="Red" Click="MyButton_Click"/>
</Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState x:Name="Blue">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="MyButton"
Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0" Value="Aqua"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
Run Code Online (Sandbox Code Playgroud)
这是Code-Behind:
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void MyButton_Click(object sender, RoutedEventArgs e)
{
VisualStateManager.GoToState(this, "Blue", true);
}
}
Run Code Online (Sandbox Code Playgroud)
理论上,这应该在点击时将Button-Color更改为"Aqua",但没有任何反应.
将内容放在内部ContentControl
并在控件上应用VisualState而不是在Page上.
而不是ObjectAnimation
,用于ColorAnimation
动画Button的颜色.
<ContentControl x:Name="contentControl">
<ContentControl.Template>
<ControlTemplate>
<Grid
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Button x:Name="MyButton"
Height="100"
Width="300"
Content="Click"
FontSize="40"
FontWeight="Bold"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Background="Red" Click="Button_Click"/>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CustomGroups">
<VisualState x:Name="Blue">
<Storyboard>
<ColorAnimation Storyboard.TargetName="MyButton"
Storyboard.TargetProperty="Background.Color"
To="Aqua"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
</ControlTemplate>
</ContentControl.Template>
</ContentControl>
Run Code Online (Sandbox Code Playgroud)
并在代码后面,传递内容控件而不是页面:
VisualStateManager.GoToState(contentControl, "Blue", true);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4928 次 |
最近记录: |