tags:

views:

541

answers:

2

Trying to implement what I thought was a simple concept. I have a user control (view) bound to a view model which provides a list of data. I have added toggle buttons to the usercontrol and would like to allow the user to use these toggle buttons to switch out which template is used to show the data. All of the templates used for the data work, and they are very different from one another so it's not just simple changes to a single template. I'd like to get this as much in XAML as possible.

Here's what I have now:

Where the data appears I have <UserControl Template="{StaticResource ListSwitchingControlTemplate}" />

In that control template I have all "sub templates" - really it's just all 3 representations with their visibility set to Collapsed. Then I use a data trigger on that control template to show the currently selected view. This works, but I noticed that all 3 representations get bound - they each act like they are active (and they are I guess).

I'd rather be able to truly switch the template at run time. I tried converting the containing user control to use a ContentTemplate for itself, but that just messes up all of the binding I have elsewhere. If only UserControls could use DataTriggers I'd be ok.

Any suggestions on how to cleanly go about getting this behavior. I have an idea that I'm just missing something simple.

Thanks, Dave

+1  A: 

One way to do this would be to use a DataTemplateSelector.

Basically, you create a class that inherits from DataTemplateSelector and override its SelectTemplate virtual function. The return value from the function is the DataTemplate you want to use and in that function you have access to the object and its properties, which you can use to decide which template to select.

There is an example on MSDN here:

http://msdn.microsoft.com/en-us/library/system.windows.controls.datatemplateselector.aspx

Trent F Guidry