私の MVVM ベースの WPF アプリケーションには、ContentControls または ContentPresenters に動的にロードされるさまざまな ViewModel タイプがたくさんあります。そのため、XAML で使用する DataTemplate を明示的に設定する必要があります。
<ContentControl Content={Binding SomePropertyOfTypeViewModel} ContentTemplate={StaticResource someTemplate} />
someTemplate
現在、私の問題は、ContentControl が何もバインドされていない (つまり、ViewModel.SomePropertyOfTypeViewModel が null である) 場合でも、コンテンツ コントロールが UI を表示していることです。暗黙の DataTemplates を使用すると、すべてが期待どおりに機能します。残念ながら、ここではこのメカニズムを使用できません。
アップデート:
bool Visible
私の現在の解決策は、親 ViewModel でプロパティとして公開されている ViewModel ごとに 1 つの追加のプロパティを持つことです。true
プロパティが null でない場合にのみ返されます。ContentControl の Visilibilty は、このプロパティにバインドされています。
ParentViewModel.SomePropertyOfTypeViewModelVisible, ParentViewModel.SomeOtherPropertyOfTypeViewModelVisible ...
<ContentControl Content={Binding SomePropertyOfTypeViewModel} Visibility={Binding SomePropertyOfTypeViewModelVisible, Converter={StaticRresource boolToVisibiltyConverter}}" />
多くの余分なプロパティを維持する必要があるため、これはあまり満足のいくものではありません。