[英]Binding a Storyboard property relative to the target of the storyboard
我有一個故事板,它以一個元素為目標,並將其自己的一個屬性綁定到另一個元素上的屬性:
<Storyboard>
<DoubleAnimation
Storyboard.TargetProperty="RenderTransform.X"
From="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=ActualWidth}"
To="0"
Duration="0:0:5"/>
</Storyboard>
當故事板存儲在保存故事板目標的窗口的資源中時,該故事板工作。 “From”值正確綁定到主機Window實例的ActualWidth。
但是,我需要將故事板存儲在我的應用程序級資源中。 從這里開始,故事板似乎無法定位窗口以確定“發件人”屬性。 這是可以理解的,因為從<Application.Resources>
內部,綁定將無法找到Window類型的'祖先'。
我想我需要能夠綁定相對於動畫目標的'From'值,而不是相對於storyboard的DoubleAnimation
。
這是可能的,如果是的話,怎么樣?
以下是MainWindow.xaml示例:
<Window.Resources>
<!--This works : Storyboard correctly sets 'From' property to 'ActualWidth' of window-->
<Storyboard x:Key="localStoryBoard">
<DoubleAnimation
Storyboard.TargetProperty="RenderTransform.X"
From="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=ActualWidth}"
To="0"
Duration="0:0:5"/>
</Storyboard>
</Window.Resources>
<StackPanel>
<Button
RenderTransformOrigin="0,1"
HorizontalAlignment="Left"
Content="Click me">
<Button.RenderTransform>
<TranslateTransform/>
</Button.RenderTransform>
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<EventTrigger.Actions>
<BeginStoryboard Storyboard="{StaticResource centralStoryBoard}"/>
</EventTrigger.Actions>
</EventTrigger>
</Button.Triggers>
</Button>
</StackPanel>
以下是app.xaml的示例:
<Application x:Class="WpfApplication3.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<!--Storyboard doesn't work at all-->
<Storyboard x:Key="centralStoryBoard">
<DoubleAnimation
Storyboard.TargetProperty="RenderTransform.X"
From="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=ActualWidth}"
To="0"
Duration="0:0:5"/>
</Storyboard>
</Application.Resources>
</Application>
這不起作用,因為eventtrigger引用了app.xaml版本。 如果將其更改為本地資源版本,則可以看到它的工作原理。
使用父級寬度的示例顯示在: ActualWidth中,作為From WPF動畫的值
如果你想要參考一個父網格的寬度你可以說
"{Binding RelativeSource={RelativeSource AncestorType={x:Type Grid}}, Path=ActualWidth}"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.