In my MVVM based WPF application I have a lot of different ViewModel types that dynamically loaded into ContentControls or ContentPresenters. Therefor I need to explictly set what DataTemplate is to be used in XAML:
<ContentControl Content={Binding SomePropertyOfTypeViewModel} ContentTemplate={StaticResource someTemplate} />
Now my problem is that the content control is displaying the UI of someTemplate
even if the ContentControl is bound to nothing (i.e ViewModel.SomePropertyOfTypeViewModel is null)
Is there a quick and easy way to make all ContentControls display nothing if they are currently bound to nothing? When I use implicit DataTemplates everything works as expected. Unfortunately I can't use this mechanism here.
Update:
My current solution is to have one extra bool Visible
property for each ViewModel that is exposed as a property in the parent ViewModels. It returns true
only when the the property is not null. The ContentControl's Visiblibilty is bound to this property.
ParentViewModel.SomePropertyOfTypeViewModelVisible, ParentViewModel.SomeOtherPropertyOfTypeViewModelVisible ...
<ContentControl Content={Binding SomePropertyOfTypeViewModel} Visibility={Binding SomePropertyOfTypeViewModelVisible, Converter={StaticRresource boolToVisibiltyConverter}}" />
This is not very satisfying because I have to maintain a lot of extra properties.