2011-01-19 24 views
1

我在尋找能夠根據哪個單選按鈕被選中來運行某個故事板的代碼。該方案是先選擇一個單選按鈕,然後單擊「下一步」按鈕運行故事板。例如,如果選擇了第一個單選按鈕並單擊了「下一步」按鈕,則應播放第一個故事板。任何想法都非常感謝。先謝謝你。如何選擇單擊單選按鈕和下一步按鈕來運行故事板?

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) { } 
    } 
} 

回答

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); 
    } 

(雖然這是從內存中,因此有可能是語法錯誤)

+0

謝謝你的領先優勢。 – vladc77 2011-01-20 00:51:35