0

I have problems with binding to this object inside VisualStateMenager. I have XAML structure:

<DataTemplate x:Key="MenuItem">
    <Border 
        x:Name="myItem"     
        Background="{StaticResource BackgroundColor}">
        <StackPanel>
            <TextBlock Text="{Binding HeaderText}" />
            <TextBlock Text="{Binding Text}" />
        </StackPanel>
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="my">
                <VisualState x:Name="active">
                    <Storyboard>
                        <ColorAnimation 
                            Storyboard.TargetName="myItem" 
                            Storyboard.TargetProperty="Background.Color"
                            To=" --- BIND TO THIS OBJECT COLOR PROPERTY ---"
                            //To="{Binding Color}" NOT WORK
                            //To="{Binding Color, ElementName=myItem}" NOT WORK
                            //To="red" WORKS
                            Duration=" 0:0:0.1"/>
                    </Storyboard>
                </VisualState>
                ...
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Border>
</DataTemplate>

DataContext for DataTemplate must be correct because Binding expression: {Binding HeaderText} and {Binding Text} works prefect. In the same DataContext I have one more property “Color”:

    public string HeaderText { get; set; }
    public string Text { get; set; }
    public string Color { get; set; }

I want bind "Color" to "ColorAnimation.To" inside animation, but somehow i lost my dataContext, and receive error:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Color; DataItem=null; target element is 'ColorAnimation' (HashCode=60716890); target property is 'To' (type 'Nullable`1')

Also it's worth to mention that everything else is good, especially state changes. Because if I remove Binding from ColorAnimation, for example write To="red". animations works.

Sonny D
  • 897
  • 9
  • 29
  • Try [this](http://stackoverflow.com/questions/3404707/access-parent-datacontext-from-datatemplate) – Mike Eason Jun 05 '15 at 12:17
  • Thank.I find a lot of links already, and read also yours. I fix problem in unpleasant tricky way, and now just wait for better solution. – Sonny D Jun 05 '15 at 13:22

1 Answers1

0

You need a converter from string to SolidColorBrush....when you specify "Red" it uses the enumerator

you can use this class

 public class StringToColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var strValue = value as string;

        if (string.IsNullOrEmpty(strValue)) return null;

        Color color = Colors.Transparent;

        try
        {
            color = (Color)ColorConverter.ConvertFromString(strValue);
        }
        catch
        {


        }
        return color;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        try
        {
            var color = (Color)value;

            if (color == null) return Colors.Transparent.ToString();

            return color.ToString();
        }
        catch 
        {

            return Colors.Transparent.ToString();
        }

    }
}
advapi
  • 3,661
  • 4
  • 38
  • 73
  • I tried your solution, I received the same error: "Cannot find...". Also tried change red to some random hex color To="#00c34d", works without any problem. – Sonny D Jun 05 '15 at 12:37
  • but does it breaks on the convert method? – advapi Jun 05 '15 at 12:38
  • No, program generally works. But when I expecting change colors nothing happen. I output show up error I mention above. – Sonny D Jun 05 '15 at 13:14
  • what I mean was : if you put a breakpoint on my code, does it breaks on it? can you please share how you use the converter? – advapi Jun 05 '15 at 13:27
  • No :D. So I create and include appropriate class in my namespace: xmlns:common="clr-namespace:App.Common" next add in resources , VS auto suggest me class so path is right. And at the end binding expression: To="{Binding ElementName=myItem, Path=Color, Converter={StaticResource StringToColorConverter}}" – Sonny D Jun 05 '15 at 16:32