3

我有一个 ToggleButton,我单击它以弹出 FlowDocumentReader 作为 Adorner。此 FlowDocument 是具有 DataTrigger 以显示/隐藏元素的 ControlTemplate 的一部分。

使用以下触发器一切正常。我使用了 DataTrigger 和一些 Setter,当我检查我的 ToggleButton 时,我的元素使用我提供的高度和宽度正确显示:

<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>

Storyboard 上下文是否与 Setter 完全不同?为什么它在一个地方有效,而在另一个地方无效?

奇怪的是,当我进行此更改时,它会导致输出窗口中显示绑定错误。我没有触及 DataTrigger 的实际绑定,只是内容:

System.Windows.Data 错误:4:找不到与引用“ElementName = adorner”进行绑定的源。BindingExpression:Path=AdornedElement.IsChecked; 数据项=空;目标元素是'控制'(名称='');目标属性是“NoTarget”(类型“对象”)

以下是模板其余部分的总体思路:

<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>
4

1 回答 1

2

I think in your case you should use Trigger instead of DataTrigger. DataTrigger is using to bind to some data in DataContext of element in which DataTemplate was applied.

I suggest you try to use Trigger in that way:

<Trigger SourceName="adorner" Property="AdornedElement.IsChecked" Value="True">
    ...
</Trigger>
于 2014-01-24T13:39:20.603 回答