2010-05-24 23 views
6

Hãy nói rằng chúng tôi có một mã XAML như thế này:WPF: Storyboard.TargetName hoạt động, nhưng Setter TargetName không

<Style TargetType="{x:Type ListBoxItem}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
       <Border HorizontalAlignment="Center" VerticalAlignment="Center"> 
        <Border.LayoutTransform> 
         <!--We are rotating randomly each image. Selected one will be rotated to 45°.--> 
         <RotateTransform Angle="{Binding RandomAngle}" x:Name="globalRotation"/> 
        </Border.LayoutTransform> 
        <Grid> 
         <Image Source="{Binding ImageLocation}" Stretch="None" /> 
         <TextBlock x:Name="title" Text="{Binding Title}" /> 
        </Grid> 
       </Border> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsSelected" Value="True"> 
         <Setter TargetName="title" Property="Visibility" Value="Visible"/> 
         <!--The next line will not compile.--> 
         <Setter TargetName="globalRotation" Property="Angle" Value="45"/> 
         <Trigger.EnterActions> 
          <BeginStoryboard> 
           <Storyboard> 
            <!--This compiles well.--> 
            <DoubleAnimation Storyboard.TargetName="globalRotation" Storyboard.TargetProperty="Angle" To="45" Duration="00:00:03"/> 
           </Storyboard> 
          </BeginStoryboard> 
         </Trigger.EnterActions> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

Mã này được thiết kế để hiển thị một tập hợp các hình ảnh trong một ListBox. Mỗi hình ảnh có một vòng quay ngẫu nhiên, nhưng khi được chọn, quay sang 45 độ.

Xoay hình ảnh đã chọn qua bảng phân cảnh hoạt động tốt. Tôi chỉ cần chỉ định Storyboard.TargetName và nó xoay hình ảnh khi được chọn (Trigger.ExitActions được bỏ qua để làm cho mã ngắn hơn).

Bây giờ, nếu tôi muốn, thay vì sử dụng một kịch bản, chuyển nhượng 45 độ giá trị trực tiếp, tôi không thể làm điều đó, bởi vì <Setter TargetName="globalRotation" Property="Angle" Value="45"/>: nó biên dịch với

"Không thể tìm được mục tiêu kích hoạt 'globalRotation'. (Mục tiêu phải xuất hiện trước bất kỳ Người định cư, Người kích hoạt hoặc Điều kiện nào sử dụng nó.) "

lỗi. Chuyện gì xảy ra? Tôi giả sử rằng Storyboard.TargetName được đánh giá trong thời gian chạy, vì vậy hãy để tôi biên dịch nó. Đúng không?

Cách làm cho nó hoạt động chỉ với một setter, mà không cần sử dụng bảng phân cảnh?

Trả lời

10

Điều này là Trigger Setter chỉ có thể nhắm mục tiêu các đối tượng FrameworkElement hoặc FrameworkContentElement, trong khi Storyboard làm việc với bất kỳ DependencyProperty nào.

Đây là giải pháp thay thế:

<Style TargetType="{x:Type ListBoxItem}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
       <Border x:Name="brd" HorizontalAlignment="Center" VerticalAlignment="Center" Tag="{Binding RandomAngle}">        
        <Border.LayoutTransform> 
         <!--We are rotating randomly each image. Selected one will be rotated to 45°.--> 
         <RotateTransform Angle="{Binding ElementName=brd, Path=Tag}" x:Name="globalRotation"/> 
        </Border.LayoutTransform> 
        <Grid> 
         <Image Source="{Binding ImageLocation}" Stretch="None" /> 
         <TextBlock x:Name="title" Text="{Binding Title}" /> 
        </Grid> 
       </Border> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsSelected" Value="True"> 
         <Setter TargetName="title" Property="Visibility" Value="Visible"/> 
         <!--The next line will not compile.--> 
         <Setter TargetName="brd" Property="Tag" Value="45"/> 
         <Trigger.EnterActions> 
          <BeginStoryboard> 
           <Storyboard> 
            <!--This compiles well.--> 
            <DoubleAnimation Storyboard.TargetName="globalRotation" Storyboard.TargetProperty="Angle" To="45" Duration="00:00:03"/> 
           </Storyboard> 
          </BeginStoryboard> 
         </Trigger.EnterActions> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
Các vấn đề liên quan