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时一定要注意同名冲突。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM