我正在将一个样式从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”的范围内找到
我做错了什么?
答案 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;
}