我一直遇到一个问题,困扰了我一段时间,但我相信我终于找到了问题所在。症状是当我的一个绑定属性触发DataTrigger
并交换ContentTemplate
时,我的WPF控件无法正确地呈现。堆栈跟踪:
System.ArgumentNullException: Value cannot be null.
Parameter name: d
at MS.Internal.Data.ElementObjectRef.GetObject(DependencyObject d, ObjectRefArgs args)
at MS.Internal.Data.ObjectRef.GetDataObject(DependencyObject d, ObjectRefArgs args)
at System.Windows.Data.BindingExpression.MS.Internal.Data.IDataBindEngineClient.VerifySourceReference(Boolean lastChance)
at MS.Internal.Data.DataBindEngine.Task.Run(Boolean lastChance)
at MS.Internal.Data.DataBindEngine.Run(Object arg)
at MS.Internal.Data.DataBindEngine.OnLayoutUpdated(Object sender, EventArgs e)
at System.Windows.ContextLayoutManager.fireLayoutUpdateEvent()
at System.Windows.ContextLayoutManager.UpdateLayout()
at System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
at System.Windows.Media.MediaContext.InvokeOnRenderCallback.DoWork()
at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
调试器并没有什么帮助,因为它只会在 application.Run()
处停止。以下是我在实际 XAML 方面所做的事情:
<CollectionViewSource x:Key="SomeCollectionView"
Source="{Binding StatsByUser}"
IsLiveSortingRequested="True">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Amount" Direction="Descending"/>
<scm:SortDescription PropertyName="Name" Direction="Ascending"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
<ItemsControl Background="Transparent" Width="{StaticResource Width}"
ItemsSource="{Binding Source={StaticResource SomeCollectionView}}">
<ItemsControl.Resources>
<DataTemplate x:Key="FullViewTemplate">
<Border Style="{StaticResource BorderStyle}">
<controls:FullCustomEntityControl CustomEntityObject="{Binding}"
Style="{StaticResource PanelStyle}"
MouseDown="Info_OnMouseDown"/>
</Border>
</DataTemplate>
<DataTemplate x:Key="CompactViewTemplate">
<Border Style="{StaticResource BorderStyle}">
<controls:CompactCustomEntityControl CustomEntityObject="{Binding}"
Style="{StaticResource PanelStyle}"
MouseDown="Info_OnMouseDown"/>
</Border>
</DataTemplate>
</ItemsControl.Resources>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ContentControl Content="{Binding}">
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="ContentTemplate" Value="{StaticResource FullViewTemplate}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=c:ShellView}, Path=ViewModel.ShowCompactView}" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource CompactViewTemplate}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
每当
ViewModel.ShowCompactView
引发PropertyChanged
事件并启动DataTrigger
时,它将切换ContentTemplate
,从而引发此错误。有没有修复这个问题或更好的架构ContentTemplate
交换的方法,以避免出现这种情况?编辑:可能相关的支持文章https://support.microsoft.com/en-us/kb/2461678 编辑2:UI发生的情况示例:
![enter image description here](https://istack.dev59.com/5tMGR.webp)
FullCustomEntityControl
,小槽是CompactCustomEntityControl
。将它们保留在任一模式下而不更改不会产生任何问题,但是使数据触发器更改它们会导致出现此类问题。此外,所使用的控件应保持一致,而不是像这里一样分割。通过将它们保留在任一模式下,我的意思是删除数据触发器并选择其中一个。编辑3:关于类似问题的帖子,微软的某位员工回复道:https://social.msdn.microsoft.com/Forums/vstudio/en-US/fb4d0f41-bfea-409f-b8ac-e66558984b7a/argumentnullexception-when-displaying-wpf-window?forum=wpf 相关信息:
如果您在堆栈上使用VerifySourceReference时收到ArgumentNullException,则绝对是由Connect 561752中描述的问题引起的。即使您的应用程序不直接使用ElementName绑定,它也可能间接使用它们 - 几个内置控件使用ElementName绑定:ComboBox、ContextMenu、MenuItem等。
ContentTemplate
是解决这个问题的可靠方法,本身不应该导致ArgumentNullException
。问题可能来自于您的控件CompactCustomEntityControl
和FullCustomEntityControl
。启用 CLR 异常抛出,然后在 VS 调试中可以获取异常抛出的实际位置。 - Novitchi S