【发布时间】:2014-05-13 17:24:09
【问题描述】:
我正在尝试通过以下方式更改所选 TabItem 的宽度:
<Style TargetType="{x:Type TabItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid x:Name="Root">
<Border x:Name="Border" Background="{TemplateBinding Background}">
<ContentPresenter x:Name="ContentSite" VerticalAlignment="Center" HorizontalAlignment="Stretch" ContentSource="Header" Margin="12,2,12,2"/>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Width"
From="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}, Mode=FindAncestor}, Path=ActualWidth}"
To="200"
Duration="0:0:1"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
</Trigger>
</Style.Triggers>
</Style>
我不得不为动画指定“From”属性,因为我没有明确指定 TabItem 的“Width”属性,它是由自定义布局容器计算的。 但是当我尝试将动画的“From”属性绑定到 TabItem 的“ActualWidth”属性时,“System.Windows.Controls.TabItem”的初始化会引发异常。我试图在“ControlTemplate”部分中移动“触发器”部分,但没有任何效果。所以我有两个问题:
1)为什么不能像我做的那样将“From”属性绑定到TabItem的ActualWidth?
2) 我怎样才能实现所需的行为?
任何提示将不胜感激。
【问题讨论】:
-
不指定
From,它的行为如何? -
@Sankarann,它抛出异常“无法使用 'System.Windows.Media.Animation.DoubleAnimation' 为 'System.Windows.Controls.TabItem' 上的 'Width' 属性设置动画”。
-
在 from 中硬编码一些值,工作正常吗?
-
是的,一切都很好。
-
@Sankarann,虽然动画现在没有抛出异常,但它的行为是不希望的,现在它在动画开始时设置 TabItem = From 的宽度,并将其更改为 To ,它不会从 TabItem 的实际宽度开始。
标签: wpf binding wpf-controls wpf-animation