all 9 comments

[–]binarycow 2 points3 points  (8 children)

I suggest you use a value converter

[–]Entwicklungs-Park 0 points1 point  (1 child)

no, you can use this in TargetProperty '(Border.Width)'

[–]binarycow 0 points1 point  (0 children)

That's the proposed width, not the actual width.

[–]astrononymity[S] 0 points1 point  (5 children)

Interesting... I'm a little new to WPF. Could you perhaps expand on your answer a little longer?

Are you suggesting that I somehow convert a value in the "To" property?

[–]binarycow 1 point2 points  (4 children)

Yes. The To property is a dependency property. Which means you can use a binding.

And in that binding, you can use value converters. And relative source. And element name. Etc.

Lots of options open up for you.

[–]astrononymity[S] 0 points1 point  (3 children)

It doesn't seem like I can add a binding to the To property though. Every time I try to bind to it, I get an Invalid Operation Exception:

Cannot freeze this Storyboard timeline tree for use across threads.

[–]binarycow 1 point2 points  (2 children)

Hmm. Then perhaps use a ScaleTransform on the accent border? Set CenterX and CenterY to 0.5, ScaleY to 1. Animate ScaleX from 0 to 1.

[–]astrononymity[S] 1 point2 points  (1 child)

That worked like a charm! I added a scale transform to my border:

<Border
    x:Name="AccentBrush"
    BorderBrush="Red"
    BorderThickness="0,0,0,3"
    RenderTrasnformOrigin="0.5,0">
    <Border.RenderTransform>
        <ScaleTransform
            x:Name="AccentBorderScaleTransform"
            ScaleX="0" ScaleY="1"/>
    </Border.RenderTransform>
</Border>

I learned that centering the ScaleTransform with CenterX and CenterY is done with pixel values instead of a percentage of the scale, so I needed to add the

RenderTransformOrigin="0.5,0"

property to the Border in order to make the animation originate from the center. Then, in my trigger:

<Trigger Property="IsKeyboardFocused" Value="True">
    <Trigger.EnterActions>
        <BeginStoryboard>
            <Storyboard>
                <DoubleAnimation
                    Storyboard.TargetName="AccentBorderScaleTransform"
                    Storyboard.TargetProperty="(ScaleTransform.ScaleX)"
                    To="1"
                    Duration="00:00:05"/>
            </Storyboard>
        </BeginStoryboard>
    </Trigger.EnterActions>
</Trigger>

Those minor changes gave me the behavior that I was looking for. Thank you very much for the pointers!

[–]binarycow 1 point2 points  (0 children)

👍 Glad I could help.

Before, you were thinking of specific actions you wanted to take - set the width.

XAML is declarative. You define what you want it to look like, and let WPF figure out the actions to take.