ビューモデルでバウンドプロパティを更新するときにNullReferenceExceptionが発生します。これは、ビューでツリービューコントロールを使用する場合にのみ発生します。私がリストに置き換えた場合、例外はなくなります。TreeViewとPropertyChangedを使用するNullReferenceException
これは私のコードでどこデバッガの休憩です:
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
30多分についての呼び出し後に、多くのPresentationFrameworkとWindowsBaseアセンブリを通じて、例外が実際にここで行われます。
これは、ツリービュー:
<TreeView DockPanel.Dock="Bottom" ItemsSource="{Binding Source={StaticResource cvs}, Path=Groups}"
ItemTemplate="{StaticResource categoryTemplate}" SelectedItemChanged="TreeView_SelectedItemChanged"/>
このリストボックスを代わりに使用すると、問題が消える:
<ListBox DockPanel.Dock="Bottom" ItemsSource="{Binding ApplicationServers}" DisplayMemberPath="Name"
SelectedItem="{Binding SelectedApplicationServer}" Height="auto"/>
私はこれが役立つことをわからないんだけど、ここで更新されますプロパティです:
:public ObservableCollection<ApplicationServer> ApplicationServers
{
get { return this._applicationServers; }
private set
{
this._applicationServers = value;
this.NotifyPropertyChanged(() => this.ApplicationServers);
}
}
そして、ここでは、そのプロパティを更新するための呼び出しです
this.ApplicationServers = new ObservableCollection<ApplicationServer>(ApplicationServerLogic.GetAll().ToList());
誰もこのような経験はありますか?私はこの問題の原因を知りませんし、リストボックスを使用するように誘惑されています。確かに、私はかなりの時点でリストボックスを使用する必要があります。これをトラブルシューティングするにはどうしたらいいですか? PresentationFrameworkアセンブリのバグですか?
また、これは私のビューのコードビハインドで、アイテム変更イベントの処理を示しています。
private void TreeView_SelectedItemChanged(object sender, System.Windows.RoutedPropertyChangedEventArgs<object> e)
{
((ApplicationServerViewModel)DataContext).SelectedApplicationServer = e.NewValue as ApplicationServer;
}
EDIT:誰かがそうここにある、より多くのコードを求め:
<CollectionViewSource x:Key="cvs" Source="{Binding ApplicationServers}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="DeploymentEnvironment"/>
</CollectionViewSource.GroupDescriptions>
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Name" Direction="Ascending"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
<!-- Our leaf nodes (server names) -->
<DataTemplate x:Key="serverTemplate">
<TextBlock Text="{Binding Path=Name}"/>
</DataTemplate>
<!-- Note: The Items path refers to the items in the CollectionViewSource group (our servers).
The Name path refers to the group name. -->
<HierarchicalDataTemplate x:Key="categoryTemplate"
ItemsSource="{Binding Path=Items}"
ItemTemplate="{StaticResource serverTemplate}">
<TextBlock Text="{Binding Path=Name}" FontWeight="Bold"/>
</HierarchicalDataTemplate>
セッターを使用してコレクションインスタンスを変更するのではなく、アイテムをコレクションに追加するだけではどうですか。コレクション変更されたイベントは、ObservableCollection ' –
ですべてのBSコードを削除しようとしましたか? 'SelectedItemChanged =" TreeView_SelectedItemChanged "'である。 'this.NotifyPropertyChanged(()=> this.ApplicationServers);'新しいPCEA( "ApplicationServers") 'に'。 'categoryTemplate'はどのように見えますか? MORE CODE PLZ –
@Aaron:あなたの提案はうまくいきました。それは私がツリービューを引き続き使用できるようになります。ありがとうございました。 –