Question

I have a bar chart that animates to the value set. The animation code looks like

<UserControl.Resources>
    <Storyboard x:Key="BootUp">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)" Storyboard.TargetName="MainBar">
            <EasingDoubleKeyFrame KeyTime="0" Value="350"/>
            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="{Binding CurrentValue, ElementName=UserControl}"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
</UserControl.Resources>

and the C#

int ThisValue=200;
public int CurrentValue
{
    get { return (int)ThisValue; }
    set { 
            ThisValue=(int)value;
            this.MainBar.ToolTip=value.ToString();
            System.Windows.Media.Animation.Storyboard storyBoard = (System.Windows.Media.Animation.Storyboard)FindResource("BootUp");
            storyBoard.Begin(this);
        }

}

I think this should work but every time it returns

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=UserControl'. BindingExpression:Path=CurrentValue; DataItem=null; target element is 'EasingDoubleKeyFrame' (HashCode=27594380); target property is 'Value' (type 'Double')

What is going on?

Was it helpful?

Solution

It should work if you change the way the binding finds the UserControl.

Replace this:

Value="{Binding CurrentValue, ElementName=UserControl}"

..with this:

Value="{Binding CurrentValue, RelativeSource={RelativeSource AncestorType=UserControl}}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top