代码隐藏中的RemoveStoryBoard崩溃了应用程序

时间:2017-09-19 11:13:23

标签: c# wpf xaml

我正在将一个样式从XAML移植到代码后面,我遇到了RemoveStoryBoard的困难。风格如下

<Style TargetType = "{x:Type telerik:GridViewCell}" x:Key="Style">
    <Style.Triggers>
        <DataTrigger Binding = "{Binding IsChanged}" Value="True">
            <DataTrigger.EnterActions>
                <RemoveStoryboard BeginStoryboardName = "Highlight" />
                <BeginStoryboard Name="Highlight">
                    <Storyboard>
                        <ColorAnimation Duration = "00:00:03" From="Red" FillBehavior="Stop" Storyboard.TargetProperty="(Control.Background).(SolidColorBrush.Color)" />
                    </Storyboard>
                </BeginStoryboard>
            </DataTrigger.EnterActions>
        </DataTrigger>
    </Style.Triggers>
</Style>

背后的代码是

private static Style CreateStyle(string color)
{
    var style = new Style(typeof(GridViewCell));

    var convertFromString = ColorConverter.ConvertFromString(color);
    if (convertFromString != null)
    {
        style.Setters.Add(new Setter(Control.BackgroundProperty,
            new SolidColorBrush((Color)convertFromString)));
    }
    else
    {
        Log.Warning("Unable to find a color for deal setting");
    }

    DataTrigger trigger = new DataTrigger
    {
        Value = true,
        Binding = new Binding
        {
            Path = new PropertyPath("IsChanged")
        }
    };

    var storyboard = new BeginStoryboard();

    var sb = new Storyboard { Name = "Highlight" };
    storyboard.Storyboard = sb;

    var colorAnimation = new ColorAnimation
    {
        Duration = new Duration(TimeSpan.FromSeconds(3)),
        From = System.Windows.Media.Colors.Red,
        FillBehavior = FillBehavior.Stop,
    };

    PropertyPath colorTargetPath = new PropertyPath("(Control.Background).(SolidColorBrush.Color)");

    Storyboard.SetTargetProperty(colorAnimation, colorTargetPath);

    var removeStoryboard = new RemoveStoryboard { BeginStoryboardName = "Highlight" };
    //  Storyboard.SetTargetProperty(removeStoryboard, colorTargetPath);

    sb.Children.Add(colorAnimation);

    trigger.EnterActions.Add(removeStoryboard);
    trigger.EnterActions.Add(storyboard);



    style.Triggers.Add(trigger);
    return style;
}

如果我评论RemoveStoryBoard一切都很好,但是当我更新另一行时,即使之前的闪烁也是如此

如果我使用RemoveStoryBoard我得到了一个异常(和调试器/客户端冻结) 告诉“突出显示”无法在“System.Windows.Style”的范围内找到

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您需要使用Style注册名称“突出显示”。您还需要设置Name对象的BeginStoryboard属性:

private static Style CreateStyle(string color)
{
    var style = new Style(typeof(GridViewCell));

    var convertFromString = ColorConverter.ConvertFromString(color);
    if (convertFromString != null)
    {
        style.Setters.Add(new Setter(Control.BackgroundProperty,
            new SolidColorBrush((Color)convertFromString)));
    }
    else
    {
        Log.Warning("Unable to find a color for deal setting");
    }

    DataTrigger trigger = new DataTrigger
    {
        Value = true,
        Binding = new Binding
        {
            Path = new PropertyPath("IsChanged")
        }
    };

    var storyboard = new BeginStoryboard() { Name = "Highlight" }; //<--

    var sb = new Storyboard { Name = "Highlight" };
    storyboard.Storyboard = sb;
    style.RegisterName("Highlight", storyboard); //<---

    var colorAnimation = new ColorAnimation
    {
        Duration = new Duration(TimeSpan.FromSeconds(3)),
        From = System.Windows.Media.Colors.Red,
        FillBehavior = FillBehavior.Stop,
    };

    PropertyPath colorTargetPath = new PropertyPath("(Control.Background).(SolidColorBrush.Color)");

    Storyboard.SetTargetProperty(colorAnimation, colorTargetPath);

    var removeStoryboard = new RemoveStoryboard { BeginStoryboardName = "Highlight" };
    //  Storyboard.SetTargetProperty(removeStoryboard, colorTargetPath);

    sb.Children.Add(colorAnimation);

    trigger.EnterActions.Add(removeStoryboard);
    trigger.EnterActions.Add(storyboard);

    style.Triggers.Add(trigger);
    return style;
}