我有一個ToggleButton,我點擊它彈出一個FlowDocumentReader作爲裝飾者。這個FlowDocument是ControlTemplate的一部分,帶有一個DataTrigger來顯示/隱藏元素。故事板不能從ControlTemplate執行DataTrigger
使用以下觸發器一切正常。我正確地使用DataTrigger和一些二傳手,我的元素顯示與高度和寬度我提供當我檢查我的切換按鈕:
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding ElementName=adorner, Path=AdornedElement.IsChecked}" Value="True" >
<Setter TargetName="mainBorder" Property="Height" Value="437"></Setter>
<Setter TargetName="mainBorder" Property="Width" Value="537"></Setter>
</DataTrigger>
</ControlTemplate.Triggers>
我想有發生是由於出現在我的元素一些動畫,所以我試圖用一個故事板。這是行不通的,似乎沒有任何事情發生:
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding ElementName=adorner, Path=AdornedElement.IsChecked}" Value="True" >
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard Storyboard.TargetName="mainBorder">
<DoubleAnimation Duration="0:0:0.2" Storyboard.TargetProperty="Width" To="537" />
<DoubleAnimationUsingKeyFrames BeginTime="0:0:0.2" Duration="0:0:0.3" Storyboard.TargetProperty="Height">
<LinearDoubleKeyFrame Value="417" KeyTime="0:0:0.2" />
<LinearDoubleKeyFrame Value="437" KeyTime="0:0:0.24" />
<LinearDoubleKeyFrame Value="417" KeyTime="0:0:0.3" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<BeginStoryboard>
<Storyboard Storyboard.TargetName="mainBorder">
<DoubleAnimationUsingKeyFrames Duration="0:0:0.2" Storyboard.TargetProperty="Width">
<LinearDoubleKeyFrame Value="0" KeyTime="0:0:0.2" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="0:0:0.2" Duration="0:0:0.2" Storyboard.TargetProperty="Height">
<LinearDoubleKeyFrame Value="0" KeyTime="0:0:0.2" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</DataTrigger.ExitActions>
</DataTrigger>
</ControlTemplate.Triggers>
故事板上下文與設置者完全不同嗎?它爲什麼在一個地方工作,而不是另一個地方?
奇怪的是,當我進行此更改時,它會導致綁定錯誤顯示在輸出窗口中。我還沒有觸及的DataTrigger實際的結合,只是內容:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=adorner'. BindingExpression:Path=AdornedElement.IsChecked; DataItem=null; target element is 'Control' (Name=''); target property is 'NoTarget' (type 'Object')
這裏是模板的其餘部分的總體思路:
<ControlTemplate x:Key="LocalHelpWindow">
<Grid>
<Grid.RowDefinitions>
...
</Grid.RowDefinitions>
<help:AdornedPlaceholder x:Name="adorner" Grid.Row="0"/>
<Border Grid.Row="1" x:Name="mainBorder">
...
</Border>
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding ElementName=adorner, Path=AdornedElement.IsChecked}" Value="True" >
...
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>