I have following ControlTemplate
:
<ContentControl Content="{Binding ContentViewModel}">
<ContentControl.Resources>
<DataTemplate DataType="{x:Type viewModel:FilledContentViewModel}">
<usercontrols:FilledMainWindow x:Name="MainContent" />
</DataTemplate>
<DataTemplate DataType="{x:Type viewModel:EmptyContentViewModel}">
<!-- todo: Something like a StartPage here -->
</DataTemplate>
</ContentControl.Resources>
</ContentControl>
This works great until the view model tries to change the ContentViewModel
property from one FilledContentViewModel
to a new FilledContentViewModel
, then the content of the ContentControl
does not refresh.
If switching from EmptyContentViewModel
to FilledContentViewModel
or the other way around, it works.
Of course, just updating everything in the existing FilledContentViewModel
instead would be one solution. But I think it could get messy quick and that just creating a new ViewModel
with the right context and switch it would be more elegant.
Does anybody know a way to let the content of the DataTemplate
refresh?
Loaded
event of theFilledMainWindow
? Or you're using one-time bindings that you want refreshed? – VediFilledMainWindow
data bound and will react accordingly if you change itsDataContext
from oneFilledContentViewModel
to another? – ScarecrowContent
to anotherFilledContentViewModel
it will changeDataContext
and re-evaluateContentTemplate
and set it again toDataTemplate
forFilledContentViewModel
but for WPF it's same template therefore not a change so it will not recreate user control and effectively only itsDataContext
will change. Guessing problem is somewhere here. – ScarecrowLoaded
event of theFilledMainWindow
and I also don't use one-time bindings anyway in the project of concern. – HeightFilledMainWindow
directly (not through DataTemplate) and will tell you the result. – HeightContentControl
just looks up the DataType and if it is still the same and only reacts if the DataType changes, too. In this project theFilledContentViewModel
gets an ORM mapper class (using Dapper) injected. So, if another database is opened on runtime I thought it would be easier to change the path to the database in the mapper class and inject it into a newFilledContentViewModel
instead of updating everything in the existing one. – HeightContentControl
, which I posted in the question by<usercontrols:FilledMainWindow DataContext="{Binding ContentViewModel}" x:Name="MainContent" />
. And then the switching of theFilledContentViewModel
objects worked as intended. The problem is: The DataTemplate for the EmptyContentViewModel, where I want to put something like a start page in future is dismissed now. – Height