C# 创建UserControl时接收InvalidOperationException
我的WPF应用程序中有一个名为AlarmItem的UserControl。以下是XAML:C# 创建UserControl时接收InvalidOperationException,c#,wpf,user-controls,C#,Wpf,User Controls,我的WPF应用程序中有一个名为AlarmItem的UserControl。以下是XAML: <UserControl x:Class="CarSystem.CustomControls.AlarmItem" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xa
<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>
将边框的可视状态设置为FlashingOn的行在执行时抛出以下InvalidOperationException:
System.InvalidOperationException:“BorderBrush”属性未指向路径“0.1”中的DependencyObject
我做错了什么?我最近添加了设置isflash属性的样式和触发器;在过去,我是在代码隐藏中执行此操作的,但当时没有发生此错误。经过大量的研究、尝试和心痛,我之所以会出现此错误,是因为Border类的BorderBrush属性是笔刷,而不是SolidColorBrush。笔刷是SolidColorBrush和所有各种渐变笔刷类派生的抽象类型
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 );
}