2017-04-20 62 views
0
<Storyboard x:Key="OnClick1"> 
     <DoubleAnimation Storyboard.TargetName="{StaticResource test}" 
       Storyboard.TargetProperty="Opacity" 
       From="1" 
       To="0" 
       RepeatBehavior="2000" 
       AutoReverse="True" 
       Duration="0:0:0.7"/> 
</Storyboard> 

<Window.Triggers> 
    <EventTrigger RoutedEvent="ButtonBase.Loaded" SourceName="button"> 
     <BeginStoryboard Storyboard="{StaticResource OnClick1}"/> 
    </EventTrigger> 
</Window.Triggers> 

<Button x:Name="button2" x:key="test" Content="Button" Margin="234,140,164,0" Height="37" VerticalAlignment="Top"/> 
<Button x:Name="button3" x:key="test" Content="Button" Margin="234,140,164,0" Height="37" VerticalAlignment="Top"/> 

我想将这个双动画添加到多个按钮而不重复双动画。最初,我将TargetName设置为第一个按钮的名称并且它可以工作,但是如果将按钮添加到按钮,我会在WinFX命名空间中收到此消息“The property”key“不存在。如何定位故事板中的一组控件WPF

回答

1

给按钮a驱动其不透明度情节提要对方按钮,

<Button 
    x:Name="button3" 
    Opacity="{Binding Opacity, ElementName=button2}" 
    ... 
    /> 

Alterna把所有的按钮都放在一个StackPanel(或者网格,或者其他)中,然后为父母的Opacity设置动画。

+0

我不明白。我还没有遇到元素名称。什么是ButtonA?它指的是什么? – Decoder94

+1

@ Decoder94查看更新。我将'ButtonA'改为'button2',因为这是您在示例中实际使用的名称。这个ElementName的东西是干什么的,它使用XAML元素作为源。其效果是,只要'button2.Opacity'发生变化,'button3'的不透明度将自动更新为具有相同的值。 –

+0

谢谢。明白! – Decoder94

1

您有示例标记发布不提供您的问题的重复的样品,但你可以触发添加到您要设置动画的每个Button

<Window x:Class="WpfApplication1.Window1" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     mc:Ignorable="d" 
     Title="Window1" Height="300" Width="300"> 
    <Window.Resources> 
     <Storyboard x:Key="OnClick1"> 
      <DoubleAnimation 
       Storyboard.TargetProperty="Opacity" 
       From="1" 
       To="0" 
       RepeatBehavior="2000" 
       AutoReverse="True" 
       Duration="0:0:0.7"/> 
     </Storyboard> 
    </Window.Resources> 
    <StackPanel> 
     <Button x:Name="button2" Content="Button" Height="37" VerticalAlignment="Top"> 
      <Button.Triggers> 
       <EventTrigger RoutedEvent="Loaded"> 
        <BeginStoryboard Storyboard="{StaticResource OnClick1}"/> 
       </EventTrigger> 
      </Button.Triggers> 
     </Button> 
     <Button x:Name="button3" Content="Button" Height="37" VerticalAlignment="Top"> 
      <Button.Triggers> 
       <EventTrigger RoutedEvent="Loaded"> 
        <BeginStoryboard Storyboard="{StaticResource OnClick1}"/> 
       </EventTrigger> 
      </Button.Triggers> 
     </Button> 
    </StackPanel> 
</Window>