C# 让组合框在WPF中闪烁
我在MVVM应用程序中有一个简单的组合框。C# 让组合框在WPF中闪烁,c#,wpf,xaml,combobox,storyboard,C#,Wpf,Xaml,Combobox,Storyboard,我在MVVM应用程序中有一个简单的组合框。 组合框内的重要内容如下所示: <ComboBox Name="EmployeesComboBox" ItemsSource="{Binding EmployeeEntries}" SelectedValuePath="Id" SelectedValue="{Binding Id}" Width="192" FontFamily=
组合框内的重要内容如下所示:
<ComboBox Name="EmployeesComboBox"
ItemsSource="{Binding EmployeeEntries}"
SelectedValuePath="Id"
SelectedValue="{Binding Id}"
Width="192" FontFamily="Arial" FontSize="12">
<ComboBox.Resources>
<Storyboard x:Key="BlinkingStoryboard">
<ColorAnimation Storyboard.TargetProperty="Background.Color" Duration="0:0:2"
From="White" To="Yellow" RepeatBehavior="Forever" AutoReverse="True" />
</Storyboard>
</ComboBox.Resources>
</ComboBox>
我甚至更改了ToggleButton的XAML样式(在组合框样式定义中),如下所示:
<Style x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border x:Name="templateRoot"
BorderBrush="{StaticResource ComboBox.Static.Border}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
<Border x:Name="splitBorder"
BorderBrush="Transparent" BorderThickness="1"
HorizontalAlignment="Right" Margin="0"
SnapsToDevicePixels="true"
Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}">
<Path x:Name="arrow" Data="F1 M 0,0 L 2.667,2.66665 L 5.3334,0 L 5.3334,-1.78168 L 2.6667,0.88501 L0,-1.78168 L0,0 Z"
Fill="{StaticResource ComboBox.Static.Glyph}"
HorizontalAlignment="Center" Margin="0"
VerticalAlignment="Center"/>
</Border>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
(我去掉了原始样式的大部分定义,只将Background=“{StaticResource ComboBox.Static.Background}”更改为Background=“{TemplateBinding Background}”
之后,我可以使用XAML中的Background属性按预期更改组合框的颜色,但我需要启动故事板,以便通过编程方式使组合框“闪烁”。
一旦我启动故事板,应用程序就会崩溃(计时器不再工作,等等),并显示错误消息。
有人知道我遗漏了什么吗?您需要在代码中获取对情节提要的引用,以便启动它。请参阅(当然,您的引用在组合框的参考资料中定义,因此您需要从中获取)
编辑:若要获取有关失败的详细信息,请在visual studio的“设置-调试/输出”窗口中启用WPF的跟踪设置,然后在输出窗口中查找任何警告/错误。从后面的代码中,我调用情节提要,找到了它,但它不起作用。我使用一个简单的情节提要动画情节提要=(情节提要)EmployeesComboBox.FindResource(“BlinkingStoryboard”);
然后animationStoryboard.Begin();
您需要获得更多信息,然后在visual studio的“设置-调试/输出”窗口中为WPF启用跟踪设置(如果已启用,请在输出窗口中查找任何警告/错误)。好的,现在我得到了一个例外,即动画没有目标。我使用Storyboard.TargetName=“ExployeesComboBox”设置了它,现在它工作正常。谢谢。请将您的答案文本更改为您的评论文本,我可以称之为解决方案;)