undermind
undermind

Reputation: 1841

WPF Animation: can't bind From property to the ActualWidth

I'm trying to change a selected TabItem's width in the following way:

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

I'm forced to specify the "From" property for the animation because I don't specify the "Width" property for the TabItem explicitly, it is calculated by the custom layout container. But when I try to bind "From" property of the animation to the "ActualWidth" property of the TabItem, the initialization of 'System.Windows.Controls.TabItem' threws an exception. I tried to move the "Triggers" section inside the "ControlTemplate" section, but with no effect. So I have 2 questions:

1) Why can't the "From" property be bound to the ActualWidth of the TabItem in the way I did it?

2) How can I achieve the desired behaviour?

Any hints would be appreciated.

Upvotes: 1

Views: 599

Answers (1)

Sankarann
Sankarann

Reputation: 2665

This cannot be done because WPF framework will freeze the things which would used for the Animation by initially and So if you are trying to Bind on a Freezable, it doest accept that. That's why it was prevented from changing and says,

Cannot freeze this Storyboard timeline tree for use across threads

Upvotes: 1

Related Questions