求助,某些环境下Style中ControlTemplate里添加事件报错

kenei 2016-11-08 03:50:59
发生一个奇怪的问题,我写了一个DataTemplate,其中一个控件使用style,Style中写了ControlTemplate,模板中一个控件绑定了一个事件.代码删掉无关属性控件,简化如下
        <DataTemplate x:Key="BarDataTemplate">
<Grid>
<telerik:RadSlider x:Name="sliderBardata"
Background="Transparent">
<telerik:RadSlider.AlternateThumbStyle>
<Style TargetType="Thumb" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Thumb" >
<Grid Height="140" ClipToBounds="False">
<Label MouseDoubleClick="Control_OnMouseDoubleClick"
Content="{Binding DataItem.Capacity}">
</Label>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</telerik:RadSlider.AlternateThumbStyle>
</telerik:RadSlider>
</Grid>
</DataTemplate>

开发环境为Win7 SP1+VS2010,该代码在我机器上和同事机器上运行正常,双击事件顺利触发,得到想要的结果.但是在客户的机器上运行就报错,环境也是Win7 SP1+VS2010.报错信息如下:
2016-11-08 07:56:36,261 [86] ERROR LogConfigStructure - 全局捕获异常params:[]
System.NullReferenceException: 未将对象引用设置到对象的实例。
在 TowerProcess.View.Control.UC_DayCapacitySet.System.Windows.Markup.IStyleConnector.Connect(Int32 connectionId, Object target)
在 System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter)
在 System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlObjectWriter objectWriter)
在 System.Windows.FrameworkTemplate.LoadOptimizedTemplateContent(DependencyObject container, IComponentConnector componentConnector, IStyleConnector styleConnector, List`1 affectedChildren, UncommonField`1 templatedNonFeChildrenField)
在 System.Windows.FrameworkTemplate.LoadContent(DependencyObject container, List`1 affectedChildren)
在 System.Windows.StyleHelper.ApplyTemplateContent(UncommonField`1 dataField, DependencyObject container, FrameworkElementFactory templateRoot, Int32 lastChildIndex, HybridDictionary childIndexFromChildID, FrameworkTemplate frameworkTemplate)
在 System.Windows.FrameworkTemplate.ApplyTemplateContent(UncommonField`1 templateDataField, FrameworkElement container)
在 System.Windows.FrameworkElement.ApplyTemplate()
在 System.Windows.FrameworkElement.MeasureCore(Size availableSize)
在 System.Windows.UIElement.Measure(Size availableSize)
在 System.Windows.Controls.Canvas.MeasureOverride(Size constraint)
在 System.Windows.FrameworkElement.MeasureCore(Size availableSize)
在 System.Windows.UIElement.Measure(Size availableSize)
在 System.Windows.ContextLayoutManager.UpdateLayout()
在 System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
在 System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
在 System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
在 System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
在 MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)

后来将此处代码调整之后,在客户机器上也运行无误,调整后代码如下:
        <DataTemplate x:Key="BarDataTemplate">
<Grid>
<telerik:RadSlider x:Name="sliderBardata" Label.MouseDoubleClick="sliderBardata_MouseDoubleClick"
Background="Transparent">
<telerik:RadSlider.AlternateThumbStyle>
<Style TargetType="Thumb" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Thumb" >
<Grid Height="140" ClipToBounds="False">
<Label
Content="{Binding DataItem.Capacity}">
</Label>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</telerik:RadSlider.AlternateThumbStyle>
</telerik:RadSlider>
</Grid>
</DataTemplate>

将双击事件放到上层DataTemplate中不再报错,但后台处理代码就要对触发源进行判断.
问题是之前的代码是有什么不对的地方么?
还是微软发布过什么补丁,修正过这种DataTemplate里嵌Style,里面再嵌ControlTemplate,里面控件再挂事件的问题?我们开发机器系统比较新,修正了这个问题,而客户机器比较旧,所以没有?
有没有了解这方面的大大,求指点

补充说明:简单描述下编写的控件,就是一个柱状图,高度可以拖动,拖动的地方有个label,显示高度的值,里面的文本随着高度的拖动而变化
...全文
334 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
kenei 2016-11-09
  • 打赏
  • 举报
回复
引用 3 楼 duanzi_peng 的回复:
style 是写到 xaml中的?
Style一般不就是写在Xaml中的么,不然在.cs文件里写?
kenei 2016-11-09
  • 打赏
  • 举报
回复
引用 2 楼 Libby1984 的回复:
报的错是“未将对象引用为实例”,肯定是什么地方有个类为null,应该是模板的某个地方用到的实例为空。 如果整个函数TowerProcess.View.Control.UC_DayCapacitySet.System.Windows.Markup.IStyleConnector.Connect(Int32 connectionId, Object target)是你写的话,你可以去里面debug看看。如果不是的话就要看看什么地方会用到它,应该是传入的值有问题。
这个方法当然不是我写的,感觉应该是wpf自己内部应用Style的方法
exception92 2016-11-09
  • 打赏
  • 举报
回复
style 是写到 xaml中的?
  • 打赏
  • 举报
回复
报的错是“未将对象引用为实例”,肯定是什么地方有个类为null,应该是模板的某个地方用到的实例为空。 如果整个函数TowerProcess.View.Control.UC_DayCapacitySet.System.Windows.Markup.IStyleConnector.Connect(Int32 connectionId, Object target)是你写的话,你可以去里面debug看看。如果不是的话就要看看什么地方会用到它,应该是传入的值有问题。
  • 打赏
  • 举报
回复
说实话,我以前这么用没出现过问题。
kenei 2016-11-09
  • 打赏
  • 举报
回复
引用 6 楼 duanzi_peng 的回复:
[quote=引用 5 楼 kenei 的回复:] [quote=引用 3 楼 duanzi_peng 的回复:] style 是写到 xaml中的?
Style一般不就是写在Xaml中的么,不然在.cs文件里写?[/quote] 一般是写在资源字典里,让后前台动态引用。 http://blog.csdn.net/muzizongheng/article/details/50775262[/quote] 打过补丁后测试通过,果然是这个问题,非常感谢,分数给你
kenei 2016-11-09
  • 打赏
  • 举报
回复
引用 6 楼 duanzi_peng 的回复:
[quote=引用 5 楼 kenei 的回复:] [quote=引用 3 楼 duanzi_peng 的回复:] style 是写到 xaml中的?
Style一般不就是写在Xaml中的么,不然在.cs文件里写?[/quote] 一般是写在资源字典里,让后前台动态引用。 http://blog.csdn.net/muzizongheng/article/details/50775262[/quote] 这个补丁我去打下试试,感觉应该就是这个问题,谢谢了
kenei 2016-11-09
  • 打赏
  • 举报
回复
引用 6 楼 duanzi_peng 的回复:
[quote=引用 5 楼 kenei 的回复:] [quote=引用 3 楼 duanzi_peng 的回复:] style 是写到 xaml中的?
Style一般不就是写在Xaml中的么,不然在.cs文件里写?[/quote] 一般是写在资源字典里,让后前台动态引用。 http://blog.csdn.net/muzizongheng/article/details/50775262[/quote] 开始我也是把Style写在资源里的,不过应为要绑定一个字段Content="{Binding DataItem.Capacity}" 写在资源里就没法绑定了
exception92 2016-11-09
  • 打赏
  • 举报
回复
引用 5 楼 kenei 的回复:
[quote=引用 3 楼 duanzi_peng 的回复:] style 是写到 xaml中的?
Style一般不就是写在Xaml中的么,不然在.cs文件里写?[/quote] 一般是写在资源字典里,让后前台动态引用。 http://blog.csdn.net/muzizongheng/article/details/50775262

8,735

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧