wpfxamldatatemplatecontentcontroldatatemplateselector

Explicitly refresh DataTemplate from a DataTemplateSelector?


I set up a ContentControl.DataTemplateSelector to my desired one.
I want that according to a command or whatever, call the ContentControl to reselect the template from the selector by either xaml or code.

Thank


Solution

  • I'm not aware of any (non-kludgy) way to do this: the DataTemplateSelector is called when WPF needs to select the template, and that's a one-off decision as far as WPF is concerned. (You can kludge it by making WPF think the content has changed, e.g. by setting the content to null and then back again -- I think that would work but haven't tested it -- but this is pretty ugly!) If there is a nice way to do this I too would be interested to know!

    However, there is an alternative way to change how content is displayed that does update in response to data changes, and that is through triggers. You can use DataTriggers in your DataTemplate.Triggers collection to show and hide elements depending on the content data. To change the entire display, you could e.g. set up two renderings in a Grid, and use triggers to control which one is visible. You could even make your data template a ContentControl, and use a trigger to change the ContentTemplate. Of course this depends on the criteria for changing the template being bindable properties, which may not always be the case.

    Here's some brief discussion of selectors vs. triggers, albeit in a slightly different context.