0

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
  • I've not found a way to do this so far without going the DataTemplateSelector route. Even there, the data within the template doesn't know a thing about how it will be presented - it cannot help in the decision of which template to show. Instead it is something outside (in my case a toggle button located elsewhere in the user control). Jun 11, 2009 at 15:54

2 Answers 2

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.