0

I have read some questions-answers explaining this topic (1,2,3) but i'm still missing something.


I have defined a `ResourceDictionary` with several `DataTemplate` for exmaple :
 <ResourceDictionary
        xmlns:wvm="clr-namespace:Octopus.Widgets.ViewModels;assembly=Octopus.Widgets"
        xmlns:wuc="clr-namespace:Octopus.Widgets.Controls;assembly=Octopus.Widgets">
    
    <DataTemplate DataType="{x:Type wvm:ClockViewModel}">
        <wuc:Clock />
    </DataTemplate>
</ResourceDictionary>

And merge it in the Application.Resources ResourceDictionary.MergedDictionaries section.
Now comes the part i don't understand.
How do I show a template in other control? I saw the use of <ContentPresenter Content="{Binding CurrentView}"/> but can't really understand it. Does the ViewModel of the container(parent) has a CurrentView property ? and what type it is then?
this is the parent-container control-view :

<Grid>
<!-- hard coded control - i want to change it -->
<Button>
    <wuc:Clock VerticalAlignment="Center" HorizontalAlignment="Center"> 
        <wuc:Clock.DataContext>
            <wvm:ClockViewModel/>
        </wuc:Clock.DataContext>
    </wuc:Clock>
</Button>

<!-- another try -->
<!--<ContentControl>
    <ContentControl.Style>
        <Style TargetType="{x:Type ContentControl}">
            <Setter Property="ContentTemplate" Value="{Binding WidgetTemplate}" />
        </Style>
    </ContentControl.Style>
</ContentControl>-->
</Grid>

Thanks
EDIT
My goal is to be able to switch widget based on key comming from DB.
Container control has a ViewModel with some properties not related to question. But maybe missing one that does and this is my question.

gilad
  • 297
  • 5
  • 21
  • please clearly describe which data (viewmodels) you have and which behavior(scenarios, use-cases) you want to achive with them. – ASh Jan 26 '22 at 08:41
  • 1
    You may just want to read the [documentation](https://learn.microsoft.com/en-us/dotnet/api/system.windows.datatemplate.datatype?view=windowsdesktop-6.0). It clearly explains how a DataTemplate is automatically chosen by its DataType. You would of course not explicitly assign a Clock to the Content of a Button, but a ClockViewModel instead. The Clock would then be created by the automatically applied DataTemplate. – Clemens Jan 26 '22 at 08:45
  • "switch widget based on key comming from DB" - ok, I can think of half a dozen ways. One of them will probably work for you. – ASh Jan 26 '22 at 08:59
  • Thanks you very much. Sometimes what might seen clear to one is chaos to another. – gilad Jan 26 '22 at 09:00

1 Answers1

1

For the generations to come here is my solution:(thanks to @clemens for putting me on track)

In the container ViewModel i added a property to hold the selected widget type

public object Content
    {
        get { return content; }
        set
        {
            content = value;
            this.RaisePropertyChanged("Content");
        }
    }

and according to Widget i needed i initialized a new ViewModel (defined in DataTemplate)
And the xaml i used :

<ContentPresenter Content="{Binding Content}"/>

Simple enough....

gilad
  • 297
  • 5
  • 21