【问题标题】:How to choose which storyboard to run on click of a Radio Button and Next button?如何在单击单选按钮和下一步按钮时选择要运行的情节提要?
【发布时间】:2011-01-19 21:50:12
【问题描述】:

我正在寻找允许根据选择的单选按钮运行某个情节提要的代码。场景是先选择一个单选按钮,然后单击“下一步”按钮运行故事板。例如,如果选择了第一个单选按钮并单击了“下一步”按钮,则应播放第一个故事板。任何想法都受到高度赞赏。提前谢谢你。

XAML:

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="ClickCount.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="640" Height="480">
<Window.Resources>
    <Storyboard x:Key="Storyboard1">
        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="border">
            <EasingColorKeyFrame KeyTime="0" Value="Red"/>
            <EasingColorKeyFrame KeyTime="0:0:0.5" Value="#FF00FFED"/>
        </ColorAnimationUsingKeyFrames>
    </Storyboard>
    <Storyboard x:Key="Storyboard2">
        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="border">
            <EasingColorKeyFrame KeyTime="0" Value="Red"/>
            <EasingColorKeyFrame KeyTime="0:0:0.5" Value="#FF5AFF00"/>
        </ColorAnimationUsingKeyFrames>
    </Storyboard>
    <Storyboard x:Key="Storyboard3">
        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="border">
            <EasingColorKeyFrame KeyTime="0" Value="Red"/>
            <EasingColorKeyFrame KeyTime="0:0:0.5" Value="#FFFFF500"/>
        </ColorAnimationUsingKeyFrames>
    </Storyboard>
</Window.Resources>

<Grid x:Name="LayoutRoot">
    <RadioButton GroupName="Os" Content="Storyboard 1" IsChecked="True" FontSize="16" Margin="10,10,10,0"/>
    <RadioButton GroupName="1" Content="Storyboard 2" Margin="10,31,0,0" FontSize="16" />
    <RadioButton GroupName="1" Content="Storyboard 3" Margin="10,50,0,0" FontSize="16" />
    <Button x:Name="Next" Content="Next&gt;" Width="75.06" Height="23" IsDefault="True" 
            Click="ClickNextButton" VerticalAlignment="Top" />
    <Border x:Name="border" BorderBrush="Black" BorderThickness="1" 
            HorizontalAlignment="Center" Height="100" VerticalAlignment="Center" 
            Width="100" Background="Red"/>
</Grid>

C#:

public partial class MainWindow : Window
    public MainWindow()
    {
        this.InitializeComponent();
    }
    private void ClickNextButton(object sender, System.Windows.RoutedEventArgs e) 
    { 
        Counter += 1; 
        if (Counter == 1) { } 
        if (Counter == 2) { } 
    } 
}

【问题讨论】:

    标签: wpf wpf-controls binding


    【解决方案1】:

    如果您将Storyboards 放在VisualStateManager 中,您可以使用GoToState 方法:

    VisualStateManager.GoToState(this, "Storyboard1", useTransitions);
    

    你的 XAML 会变成这样:

    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup Name="MyStates">
            <VisualState Name="Storyboard1">
                <Storyboard>
                    <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="border">
                        <EasingColorKeyFrame KeyTime="0" Value="Red"/>
                        <EasingColorKeyFrame KeyTime="0:0:0.5" Value="#FF00FFED"/>
                    </ColorAnimationUsingKeyFrames>
                 </Storyboard>
              </VisualState>
              ....
        </VisualStateGroup>
      </VisualStateManager.VisualStateGroups>
    

    请注意,名称已从 Storyboard 移动到 VisualState

    你的代码是这样的:

        if (Counter == 1)
        {
            VisualStateManager.GoToState(this, "Storyboard1", useTransitions);
        } 
        if (Counter == 2)
        {
            VisualStateManager.GoToState(this, "Storyboard2", useTransitions);
        } 
    

    (虽然这是来自记忆,所以可能存在语法错误)

    【讨论】:

      猜你喜欢
      • 2014-11-26
      • 1970-01-01
      • 2013-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-30
      • 2014-04-09
      • 2012-04-13
      相关资源
      最近更新 更多