WPF: Button ControlTemplate + space = exception?

I put together a template in Expression Blend for a button and it works fine - until I navigate to it and hit the spacebar. I am getting the following error:

InvalidOperationException Property [Unknown] 'does not point to a DependencyObject in path' (0). (1). [0]. (2) '.

The code looks like this:

    <ControlTemplate x:Key="EmailButton" TargetType="{x:Type Button}">
        <ControlTemplate.Resources>
            <Storyboard x:Key="MouseOverStoryboard">
                <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)">
                    <SplineColorKeyFrame KeyTime="00:00:00.4000000" Value="#FF55679E"/>
                </ColorAnimationUsingKeyFrames>
            </Storyboard>
            <Storyboard x:Key="UnMouseOverStoryboard">
                <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)">
                    <SplineColorKeyFrame KeyTime="00:00:00.4000000" Value="#FF92A2D3"/>
                </ColorAnimationUsingKeyFrames>
            </Storyboard>
        </ControlTemplate.Resources>
        <Border x:Name="border" BorderBrush="#FF000000" BorderThickness="2,2,2,2" CornerRadius="4,4,4,4">
            <Border.Background>
                <LinearGradientBrush EndPoint="0.47,-0.01" StartPoint="0.47,0.808">
                    <GradientStop Color="#FF92A2D3" Offset="0"/>
                    <GradientStop Color="#FFFFFFFF" Offset="1"/>
                </LinearGradientBrush>
            </Border.Background>
            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Margin="{TemplateBinding Padding}"/>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsPressed" Value="True">
                <Setter Property="Background" TargetName="border" Value="#FF92A2D3"/>
            </Trigger>
            <Trigger Property="IsMouseOver" Value="True">
                <Trigger.EnterActions>
                    <BeginStoryboard x:Name="MouseOverStoryboard_BeginStoryboard" Storyboard="{StaticResource MouseOverStoryboard}"/>
                </Trigger.EnterActions>
                <Trigger.ExitActions>
                    <BeginStoryboard x:Name="UnMouseOverStoryboard_BeginStoryboard" Storyboard="{StaticResource UnMouseOverStoryboard}"/>
                </Trigger.ExitActions>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

      

It seems that either MouseOverStoryboard or UnMouseOverStoryboard is fired (the path mentioned in the error seems to match the TargetProperty). I'm not sure why I either fired when I hit the spacebar though ... Any suggestions?

EDIT: Thanks to Ryan Versaw, I tried the following, which now prevents the spacebar from being pressed when throwing an exception:

            <Trigger Property="IsPressed" Value="True">
                <Setter Property="Background" TargetName="border">
                    <Setter.Value>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FF92A2D3" Offset="0"/>
                            <GradientStop Color="#FF92A2D3" Offset="1"/>
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
            </Trigger>

      

The brush type now remains consistent (LinearGradientBrush), and there is always 0 available GradientStop.

I am still interested in knowing one more thing: what is the best to solve this problem, given that this sorta is like a hack (eg: added / animated third gradient stop, then a third GradientStop must be added when IsPressed == True) ...

Thanks for the suggestions! Hope this helps someone else too.

+1


a source to share


3 answers


I'm not sure what the optimal solution would be, but I found one that works.

Create LinearGradientBrush

in your resources:

<LinearGradientBrush x:Key="solidBrush">
    <GradientStop Color="#FF92A2D3"/>
</LinearGradientBrush>

      



Modify the trigger IsPressed

as follows (changes only Value

):

<Trigger Property="IsPressed" Value="True">
    <Setter Property="Background" TargetName="border" Value="{StaticResource solidBrush}"/>
</Trigger>

      

This should change the entire background to a new one (which it also is LinearGradientBrush

).

+1


a source


If you press the spacebar, then press the button. You should look at the triggerIsPressed



+1


a source


It looks like your IsPressed Trigger Setter trigger is trying to assign a color value (# FF92A2D3) to the Background property of your border. However, the background property of your border contains a linear gradient brush, not a solid color brush. However, this is only a glance.

+1


a source







All Articles