I have a DataTemplate(well two data templates) that I want to use as views for some
basic form viewmodels(that that contain a value and and boolean indicating whether I want to use the value).
I want to use the datatemplate(s) several times for separate form items. I think the right way to do this is to set it as the ContentControl’s ContentTemplate (in that case it will have the same data context right?) but I also want to pass the label string and since the label string is part of the ui and doesn’t change it seems wrong to put it in the viewmodel object. How do I give access of the label string to the DataTemplate instance?
Just like its name, a
DataTemplateis used to template the Data… For example, if you have a class calledMyItemwhich has aNameandValueand you want this shown in a specific way, you’ll set a datatemplate for Item and use it whenever needed.In your case, you’re speaking about having very similar
views, with only a minor change between them. This minor change (if I understood your question correctly) is not something that comes from the model or from the viewmodel but something which is entirely view-oriented (a different title for the page, for instance).If you plan on using a different viewmodel for every view, and each viewmodel has a different purpose – I don’t see a problem with adding a
Titleproperty to the VM and bind to that too (Remember, MVVM is a set of guidelines, not rules…)If you still rather have it separated from the viewmodel, then you can use an Attached Property. Create an Attached Property called
TemplateTitle, for instance, and have each contentcontrol in each view change it. The label, of course, will bind to that Attached Property.