WPF System.InvalidCastException: 无法将类型为“System.Windows.Media.Color”的对象强制转换为类型“System.Windows.Media.Brush”。

场景:添加ComboBox样式,界面卡死,日志异常文件如下:

2018-04-08 20:07:44,641 | ERROR | System.InvalidCastException: 无法将类型为“System.Windows.Media.Color”的对象强制转换为类型“System.Windows.Media.Brush”。
   在 System.Windows.Shapes.Shape.get_Stroke()
   在 System.Windows.Shapes.Shape.get_IsPenNoOp()
   在 System.Windows.Shapes.Shape.GetPen()
   在 System.Windows.Shapes.Shape.GetNaturalSize()
   在 System.Windows.Shapes.Shape.MeasureOverride(Size constraint)
   在 System.Windows.FrameworkElement.MeasureCore(Size availableSize)
   在 System.Windows.UIElement.Meas

原因:将颜色值赋给Brush,错误代码如下:

<Setter Property="BorderBrush" Value="{DynamicResource Blue5}" />

解决方法:静态引用正确的样式,或者删除该代码。使用DynamicResource时一定要注意同名冲突。

posted @ 2018-04-08 20:15  致林  阅读(3624)  评论(0编辑  收藏  举报