当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - DisconnectedItem in .NET 4.5.1

问题描述:

I have been searching for the DisconnectedItem issue over the Internet and I have learned that it should have been a solved problem in 4.5 release of .NET Framework. I am currently using 4.5.1 and it's my first time to face this kind of malfunction. The scenario is I have a ViewModel and a View. After an entity changed event (nHibernate) I dispose the old ViewModel, create a new one of the same type and attach it to already existing View (resolved form the Unity container). The problem is that the View has an ItemsControl with ItemsSource bound to the ViewModel List. After attaching the new ViewModel, the DelegateCommand refreshes the CanExecute methods. At first I get the objects, but than comes a sequence of DisconnectedItems (so even though I return false if DisconnectedItem, I'd get all buttons blocked). Is there any other way of changing the DataContext without creating a brand new View?

Here is the "after entity changed" part. The reportFilesCollection is bound to ItemsControl:

_detailsViewModel.Dispose();

_detailsViewModel = new DetailsViewModel(reportFilesCollection, _unityContainer);

IDetailsView view = GetViewOfTypeFromRegion<IDetailsView>();

view.ViewModel = _detailsPreviewViewModel;

And the View part:

<StackPanel x:Name="reportDataPart">

<ItemsControl ItemsSource="{Binding ReportFiles}"

Style="{StaticResource IconDataTableStyle}">

<ItemsControl.ItemTemplate>

<DataTemplate>

<ItemsControl Style="{StaticResource IconDataStyle}"

Tag="PrinterPath">

<Label ToolTip="Report generation time"

Content="{Binding CreatedOn, StringFormat={}{0:g}}"/>

<Button Content="Sign report"

Command="{Binding DataContext.SignReportCommand, ElementName=reportDataPart}"

CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=DataContext}"/>

</ItemsControl>

</DataTemplate>

</ItemsControl.ItemTemplate>

</ItemsControl>

</StackPanel>

The second ItemsControl (the inner one) provides a special style for the row of data. The absence of it doesn't make any difference. After I reenter the View, all of the buttons are active again but DisconnectedItems are still being present.

分享给朋友:
您可能感兴趣的文章:
随机阅读: