2017-06-29 39 views
0

這裏我有在機應用資源WPF樣式故事板的另一對象的價值

<Style x:Key="ClickableText" TargetType="{x:Type Button}"> 
     <Setter Property="BorderBrush" Value="{x:Null}"/> 
     <Setter Property="FontFamily" Value="/Tasks;component/Assets/Fonts/#Abel"/> 
     <Setter Property="Background" Value="Transparent" /> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type Button}"> 
        <Border Background="{TemplateBinding Background}"> 
         <StackPanel VerticalAlignment="Center"> 
          <ContentPresenter x:Name="Text" HorizontalAlignment="Center" VerticalAlignment="Center"/> 
          <Rectangle x:Name="Rect1" Width="{Binding ActualWidth, ElementName=Text}" Height="2" Fill="{DynamicResource LightGrey}"/> 
         </StackPanel> 
        </Border> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsMouseOver" Value="True"> 
          <Setter Property="Background" Value="White"/>         
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

在我添加了一個強調矩形在按鈕上的文本樣式按鈕樣式。

<Rectangle x:Name="Rect1" Width="{Binding ActualWidth, ElementName=Text}" Height="2" Fill="{DynamicResource LightGrey}"/> 

我已將矩形寬度綁定爲與文本相同的寬度,以便它添加下劃線效果。

我現在想,這樣當你將鼠標懸停在按鈕矩形揭示了通過劈裂出添加效果。

我已經遠遠被觸發變量

<Trigger.EnterActions> 
    <BeginStoryboard> 
     <Storyboard> 
      <DoubleAnimation Duration="0:0:0.300" From="0" To="{Binding ActualWidth, ElementName=Text}" Storyboard.TargetName="Rect1" Storyboard.TargetProperty="Width" /> 
     </Storyboard> 
    </BeginStoryboard> 
</Trigger.EnterActions> 
<Trigger.ExitActions> 
    <BeginStoryboard> 
     <Storyboard> 
      <DoubleAnimation Duration="0:0:0.300" From="{Binding ActualWidth, ElementName=Text}" To="0" Storyboard.TargetName="Rect1" Storyboard.TargetProperty="Width" /> 
     </Storyboard> 
    </BeginStoryboard> 
</Trigger.ExitActions> 

我要鏈接的往返雙動畫的一部分,我在矩形使用的結合,但它不斷產生錯誤的同時加入該得到這個。我怎樣才能做到這一點?

我也想用這個作爲一個可重用的風格,我可以分發和保持應用程序資源。我看到其他人在代碼做到這一點通過變通方法,但我不知道你是否能在應用程序資源

任何幫助或指導,這樣做大大appriciated!

+0

沒有看過整個問題,但基本上'Binding'非常的'StoryBoard'元素的限制,通常失敗和解決方法只存在對於一些屬性。 – grek40

+0

[WPF動畫:綁定到故事板動畫的「To」屬性]的可能重複(https://stackoverflow.com/questions/2186933/wpf-animation-binding-to-the-to-attribute-of-storyboard-動畫) – grek40

回答

1

LayoutTransform動畫更適合這種效果。 我會做這個動畫這樣:

<Style x:Key="ClickableText" TargetType="{x:Type Button}"> 
<Setter Property="BorderBrush" Value="{x:Null}"/> 
<Setter Property="FontFamily" Value="/Tasks;component/Assets/Fonts/#Abel"/> 
<Setter Property="Background" Value="Transparent" /> 
<Setter Property="Template"> 
    <Setter.Value> 
     <ControlTemplate TargetType="{x:Type Button}"> 
      <Border x:Name="MainBorder" Background="{TemplateBinding Background}"> 
       <Grid HorizontalAlignment="Center" VerticalAlignment="Center"> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="Auto"/> 
         <RowDefinition Height="2"/> 
        </Grid.RowDefinitions> 
        <ContentPresenter x:Name="Text" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center"/> 
        <Rectangle x:Name="Rect1" Grid.Row="1" Height="2" Width="{Binding ElementName=Text, Path=ActualWidth}" Fill="LightGray"> 
         <Rectangle.LayoutTransform> 
          <ScaleTransform ScaleX="0"/> 
         </Rectangle.LayoutTransform> 
        </Rectangle> 
       </Grid> 
      </Border> 
      <ControlTemplate.Triggers> 
       <Trigger Property="IsMouseOver" Value="True"> 
        <Setter Property="Background" TargetName="MainBorder" Value="White"/> 
        <Trigger.EnterActions> 
         <BeginStoryboard> 
          <Storyboard> 
           <DoubleAnimation Storyboard.TargetName="Rect1" Storyboard.TargetProperty="(ContentPresenter.LayoutTransform).(ScaleTransform.ScaleX)" From="0" To="1" Duration="0:0:0.2" /> 
          </Storyboard> 
         </BeginStoryboard> 
        </Trigger.EnterActions> 
        <Trigger.ExitActions> 
         <BeginStoryboard> 
          <Storyboard> 
           <DoubleAnimation Storyboard.TargetName="Rect1" Storyboard.TargetProperty="(ContentPresenter.LayoutTransform).(ScaleTransform.ScaleX)" From="1" To="0" Duration="0:0:0.2" /> 
          </Storyboard> 
         </BeginStoryboard> 
        </Trigger.ExitActions> 
       </Trigger> 
      </ControlTemplate.Triggers> 
     </ControlTemplate> 
    </Setter.Value> 
</Setter>