我有一些代码,当它执行时,它抛出一个NullReferenceException,说:
你调用的对象是空的.
这是什么意思,我该怎么做才能解决这个错误?
我有一个带有图像的Row的数据网格.此图像与某个状态的触发器绑定.当状态改变时我想改变图像.
模板本身在DataGridTemplateColumn的HeaderStyle上设置.此模板具有一些绑定.第一个绑定日显示它是什么日期,状态通过触发器更改图像.
这些属性在ViewModel中设置.
属性:
public class HeaderItem
{
public string Day { get; set; }
public ValidationStatus State { get; set; }
}
this.HeaderItems = new ObservableCollection<HeaderItem>();
for (int i = 1; i < 15; i++)
{
this.HeaderItems.Add(new HeaderItem()
{
Day = i.ToString(),
State = ValidationStatus.Nieuw,
});
}
Run Code Online (Sandbox Code Playgroud)
数据网格:
<DataGrid x:Name="PersoneelsPrestatiesDataGrid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
AutoGenerateColumns="False" SelectionMode="Single" ItemsSource="{Binding CaregiverPerformances}" FrozenColumnCount="1" >
<DataGridTemplateColumn HeaderStyle="{StaticResource headerCenterAlignment}" Header="{Binding HeaderItems[1]}" Width="50">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{ Binding Performances[1].Duration,Converter={StaticResource timeSpanConverter},Mode=TwoWay}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock TextAlignment="Center" Text="{ Binding …Run Code Online (Sandbox Code Playgroud) 我有一个带有两个ICollectionViews 的ViewModel,它们被绑定为ItemsSource两个不同的ListBoxes.两者都包装相同ObservableCollection,但使用不同的过滤器.最初一切正常,两个ListBox都正确填充.
但是,当我更改ObservableCollection中的项并修改与过滤相关的属性时,ListBoxes不会更新.在调试器中,我发现两个ICollectionVIews的SourceCollection都为null,尽管我的ObservableCollection仍然存在.
这是我修改项目的方法,确保通过删除和添加相同的项目来更新ICollectionViews:
private void UnassignTag(TagViewModel tag)
{
TrackChangedTagOnCollectionViews(tag, t => t.IsAssigned = false);
}
private void TrackChangedTagOnCollectionViews(TagViewModel tag, Action<TagViewModel> changeTagAction)
{
_tags.Remove(tag);
changeTagAction.Invoke(tag);
_tags.Add(tag);
}
Run Code Online (Sandbox Code Playgroud)
该机制适用于我使用相同类的另一个上下文.
此外,我意识到如果我在ICollectionViews的CollectionChanged事件上注册监听器,问题就会消失.我确保从GUI线程创建和修改它们并怀疑垃圾收集是问题所在,但目前我被卡住了......想法?
更新:
在调试时我意识到在我调用ShowDialog()托管UserControl的WinForms表单之前,SourceCollections仍然存在.显示对话框后,它们就消失了.
我像这样创建ICollectionViews:
AvailableTags = new CollectionViewSource { Source = _tags }.View;
AssignedTags = new CollectionViewSource { Source = _tags }.View;
Run Code Online (Sandbox Code Playgroud)
这是我如何绑定其中一个(另一个非常相似):
<ListBox Grid.Column="0" ItemsSource="{Binding AvailableTags}" Style="{StaticResource ListBoxStyle}">
<ListBox.ItemTemplate>
<DataTemplate>
<Border Style="{StaticResource ListBoxItemBorderStyle}">
<DockPanel>
<Button DockPanel.Dock="Right" ToolTip="Assign" Style="{StaticResource IconButtonStyle}"
Command="{Binding Path=DataContext.AssignSelectedTagCommand, …Run Code Online (Sandbox Code Playgroud) 我在LifeShaping过滤的PresentationFramework中得到一个空引用:
堆栈跟踪没有给我很多线索:
at System.Windows.Data.ListCollectionView.RestoreLiveShaping()
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)
at System.Windows.Threading.DispatcherOperation.InvokeImpl()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.ProcessQueue()
at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, …Run Code Online (Sandbox Code Playgroud)