我正在尝试通过以下方式更改所选 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)我怎样才能达到预期的行为?
任何提示将不胜感激。