创建UserControl时接收InvalidOperationException

时间:2014-06-05 21:16:18

标签: c# wpf user-controls

我的WPF应用程序中有一个名为UserControl的{​​{1}}。这是XAML:

AlarmItem

此控件背后的代码中包含以下代码,该代码在<UserControl x:Class="CarSystem.CustomControls.AlarmItem" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:cs="clr-namespace:CarSystem.CustomControls" mc:Ignorable="d" d:DesignHeight="100" d:DesignWidth="100" DataContext="{Binding Path=Alarm, RelativeSource={RelativeSource Self}}"> <UserControl.Resources> <Style TargetType="{x:Type cs:AlarmItem}"> <Setter Property="IsFlashing" Value="False" /> <Setter Property="FlashColor" Value="{DynamicResource NotPendingAlarmBorder}" /> <Style.Triggers> <DataTrigger Binding="{Binding Path=IsExpired}" Value="True"> <Setter Property="FlashColor" Value="{DynamicResource ExpiredAlarmBorder}" /> <Setter Property="IsFlashing" Value="True" /> </DataTrigger> <DataTrigger Binding="{Binding Path=IsPending}" Value="True"> <Setter Property="IsFlashing" Value="True" /> </DataTrigger> <DataTrigger Binding="{Binding Path=IsWhiteListAlarm}" Value="True"> <Setter Property="FlashColor" Value="{DynamicResource WhiteListAlarmBorder}" /> </DataTrigger> </Style.Triggers> </Style> <Style x:Key="AlarmItemBorderStyle" TargetType="Border"> <Setter Property="BorderThickness" Value="2" /> <Style.Triggers> <DataTrigger Binding="{Binding Path=IsExpired}" Value="True"> <Setter Property="BorderThickness" Value="4" /> </DataTrigger> <DataTrigger Binding="{Binding Path=IsPending}" Value="True"> <Setter Property="BorderThickness" Value="4" /> </DataTrigger> </Style.Triggers> </Style> </UserControl.Resources> <Border HorizontalAlignment="Center" Margin="5" Height="100" Name="Border" Style="{StaticResource AlarmItemBorderStyle}" VerticalAlignment="Center" Width="100"> <Border.Resources> <Storyboard x:Key="FlashingStoryboard" AutoReverse="False" RepeatBehavior="Forever"> <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:01" Storyboard.TargetName="Border" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)"> <DiscreteColorKeyFrame KeyTime="00:00:00" Value="Black" /> <DiscreteColorKeyFrame KeyTime="00:00:00.5" Value="{Binding Path=FlashColor, RelativeSource={RelativeSource AncestorType={x:Type cs:AlarmItem}}}" /> </ColorAnimationUsingKeyFrames> </Storyboard> </Border.Resources> <VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name="FlashStates"> <VisualState x:Name="FlashingOn" Storyboard="{StaticResource ResourceKey=FlashingStoryboard}" /> <VisualState x:Name="FlashingOff" /> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <Grid> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Image Grid.Row="0" Name="AlarmImage" Source="{Binding Path=Image, RelativeSource={RelativeSource AncestorType={x:Type cs:AlarmItem}}}" Stretch="Fill" /> <cs:ResponseTimer Expired="Timer_Expired" Grid.Row="1" HideIfExpired="True" IsTabStop="False" MinHeight="10" x:Name="TheTimer" TimeoutPeriod="00:02:30" VerticalAlignment="Bottom" /> </Grid> </Border> </UserControl> 属性更改时运行:

IsFlashing

将Border设置为private void OnIsFlashingChanged( object sender, bool flashNow ) { if ( flashNow ) { if ( !VisualStateManager.GoToElementState( Border, "FlashingOn", true ) ) { Log.Debug( "AlarmItem.xaml.cs: Visual State Manager transition failed." ); } } else { if ( !VisualStateManager.GoToElementState( Border, "FlashingOff", true ) ) { Log.Debug( "AlarmItem.xaml.cs: Visual State Manager transition failed." ); } } } private static void OnIsFlashingInvalidated( DependencyObject d, DependencyPropertyChangedEventArgs e ) { AlarmItem alarmItem = (AlarmItem) d; alarmItem.OnIsFlashingChanged( d, (bool) e.NewValue ); } 的可视状态的行在执行时会抛出以下FlashingOn

  

System.InvalidOperationException:'BorderBrush'属性不指向路径'(0)。(1)'中的DependencyObject。

我做错了什么?我最近添加了InvalidOperationException和设置Style属性的触发器;在过去,我在代码隐藏中做了这个,然后没有发生这个错误。

1 个答案:

答案 0 :(得分:0)

经过大量研究,试验和试验错误和心痛,我收到错误的原因是因为Border类的BorderBrush属性是Brush,而不是SolidColorBrushBrush是一个抽象类型,SolidColorBrush和所有各种渐变画笔类都来自。