2014-01-26 42 views
0

今天是我開始使用WPF的好日子,這是我創建的啓動器。使用下面的代碼 ,我設法得到的結果的截圖中可以看出:訪問ItemsControl項目並逐個動畫

<Grid> 
     <ItemsControl ItemsSource="{Binding Programs}"> 
      <ItemsControl.ItemsPanel> 
       <ItemsPanelTemplate> 
        <WrapPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center" /> 
       </ItemsPanelTemplate> 
      </ItemsControl.ItemsPanel> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <Button Content="{Binding Text}" Background="Transparent" Foreground="White" Width="128" Height="150" > 
         <Button.RenderTransform> 
          <TransformGroup> 
           <ScaleTransform /> 
          </TransformGroup> 
         </Button.RenderTransform> 
         <Button.Template> 
          <ControlTemplate TargetType="Button"> 
           <Grid> 
            <Grid.RowDefinitions> 
             <RowDefinition Height="*" /> 
             <RowDefinition Height="Auto" /> 
            </Grid.RowDefinitions> 

            <Image Grid.Row="0" Source="{Binding Image}" Height="128" /> 
            <ContentPresenter Grid.Row="1" HorizontalAlignment="Center" Margin="3,10" /> 
            <Rectangle Grid.Row="0" Fill="{TemplateBinding Background}" /> 
            <Rectangle Grid.Row="1" Fill="{TemplateBinding Background}" /> 
           </Grid> 
          </ControlTemplate> 
         </Button.Template> 
         <Button.Resources> 
          <Storyboard SpeedRatio="4" x:Key="MouseEnterStoryboard" x:Name="MouseEnterStoryboard"> 
           <ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" To="#22FFFFFF"></ColorAnimation> 
          </Storyboard> 
          <Storyboard SpeedRatio="4" x:Key="MouseLeaveStoryboard" x:Name="MouseLeaveStoryboard"> 
           <ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" To="Transparent"></ColorAnimation> 
          </Storyboard> 
          <Storyboard Duration="00:00:00.05" x:Key="MouseClickStoryboard" AutoReverse="True"> 
           <DoubleAnimation To="0.8" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"/> 
           <DoubleAnimation To="0.8" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"/> 
          </Storyboard> 
          <Storyboard x:Key="WindowLoadedStoryboard"> 
           <DoubleAnimation Storyboard.TargetProperty="Opacity" From="0" To="1" Duration="00:00:01" /> 
          </Storyboard> 
         </Button.Resources> 
         <Button.Triggers> 
          <EventTrigger RoutedEvent="Mouse.MouseEnter"> 
           <BeginStoryboard Storyboard="{StaticResource MouseEnterStoryboard}" /> 
          </EventTrigger> 
          <EventTrigger RoutedEvent="Mouse.MouseLeave"> 
           <BeginStoryboard Storyboard="{StaticResource MouseLeaveStoryboard}" /> 
          </EventTrigger> 
          <EventTrigger RoutedEvent="Button.Click"> 
           <BeginStoryboard Storyboard="{StaticResource MouseClickStoryboard}" /> 
          </EventTrigger> 
          <EventTrigger RoutedEvent="Window.Loaded"> 
           <BeginStoryboard Storyboard="{StaticResource WindowLoadedStoryboard}"></BeginStoryboard> 
          </EventTrigger> 
         </Button.Triggers> 
        </Button> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 
    </Grid> 

截圖:

enter image description here

現在,綁定到該控件列表中的每個項目,它會創建一個按鈕。 我將如何以編程方式訪問此按鈕,更好的是,我將如何以編程方式訪問故事板之一,因爲分配一個名稱(x:他們根本就不會這樣做的伎倆似乎...

另外,如何可以我一個接一個地設置按鈕的動畫效果?目前它們每個都在同一時間淡入(@ WindowLoadedStoryboard),但是我想讓每個按鈕都以一個很短的延遲逐漸消失,以創建一個很好的效果。實現這一目標?

希望有人能回答這些問題2我!

問候!

+0

有沒有人看到這個問題? –

+0

請不要在這裏提出這些問題......您可以通過查看顯示頁面查看統計信息的頁面部分(右上角),看到人們*看到了您的問題。 – Sheridan

回答

1

您訪問DataTemplate中定義的元素的問題是由於您在DataTemplate中定義了這些元素造成的......這些元素可能會在許多不同類型的UI容器控件中呈現。您可以從MSDN的How to: Find DataTemplate-Generated Elements頁面找到解決方案。

您首先需要掌握包含已應用DataTemplate的項目的相關容器控件。接下來,您需要從該容器控件中獲取ContentPresenter,然後您可以從ContentPresenter獲取DataTemplate。最後,您可以從DataTemplate訪問指定的元素。從鏈接頁面:

// Getting the currently selected ListBoxItem 
// Note that the ListBox must have 
// IsSynchronizedWithCurrentItem set to True for this to work 
ListBoxItem myListBoxItem = (ListBoxItem)(myListBox.ItemContainerGenerator. 
    ContainerFromItem(myListBox.Items.CurrentItem)); 

// Getting the ContentPresenter of myListBoxItem 
ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(myListBoxItem); 

// Finding textBlock from the DataTemplate that is set on that ContentPresenter 
DataTemplate myDataTemplate = myContentPresenter.ContentTemplate; 
TextBlock myTextBlock = 
    (TextBlock)myDataTemplate.FindName("textBlock", myContentPresenter); 

// Do something to the DataTemplate-generated TextBlock 
MessageBox.Show("The text of the TextBlock of the selected list item: " + 
    myTextBlock.Text);