.NET Framework Developer Center > .NET Development Forums > Windows Presentation Foundation (WPF) > DataTemplateSelector criterion versus DataTemplate DataContext
Ask a questionAsk a question
 

AnswerDataTemplateSelector criterion versus DataTemplate DataContext

  • Friday, June 15, 2007 3:50 PMbuzzweetman Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     

    This may apply to other places were a DataTemplateSelector is used, but my current problem is with a ContentControl's ContentTemplateSelector.

     

    In my ContentControl I specify a ContentTemplateSelector... which is a DataTemplateSelector..

    Note: I haven't been able to find detailed enough documentation and/or examples for my needs, so what I've learned is through experimentation.

    It looks to me like the DataTemplateSelector's SelectTemplate method's first parameter, which is an object, comes from whatever I bind the ContentControl's Content property to.

    So whatever I bind Content to should be the criterion that I use to choose my DataTemplate in the SelectTemplate method.

    But... it also looks like the DataContext of the DataTemplate I choose will ALSO be whatever I bind Content to!

    For me, this is a problem...

    Say I have an object called MyBusinessObject.  It has a property MyObjectTypeID.  Based on MyObjectTypeID, I want to choose a specific DataTemplate.  So I bind my ContentControl's Content to this property.  Now I am choosing the correct DataTemplate in SelectTemplate.

    But the DataContent of my chosen DataTemplate is also going to be MyObjectTypeID.  That's no good.  I need the entire MyBusinessObject!

     

    My workaround (which may seem obvious) is to bind the ContentControl's Content to MyBusinessObject.  Then within SelectTemplate I can simply access the MyObjectTypeID property within MyBusinessObject and make my DataTemplate choice.  And now, since Content is binded to MyBusinessObject, this will be the selected DataTemplate's DataContext, and I'm all set.  But wait...

     

    If I bind Content to MyBusinessObject, and only the MyObjectTypeID property within it gets changed (by some code and/or binding somewhere)... the ContentControl's Content will not know of this change and won't re-evaluate its ContentTemplateSelector. It will only reevaluate when the entire MyBusinessObject object changes.  Content really needs to be bound to MyObjectTypeID... but as described above, it really needs to be bound to MyBusinessObject!

     

    So I used MultiBinding and binded Content to both.  I bind to MyBusinessObject.MyObjectTypeID so there will be evaluation when this property changes, and I bind to MyBusinessObject so this object will be available as my DataTemplate's DataContext.  With Multibinding I need a converter.  And in this case, all it need to do is to return the MyBusinessObject that was passed into it, and ignore the MyObjectTypeID.  Now Content's value is MyBusinessObject and the ContentTemplateSelector's SelectTemplate method uses it to check the MyObjectTypeID property and choose a template.  And since Content is MyBusinessObject, the chosen DataTemplate with have this as it's DataContext.

     

    Yay!  (sort of)

    There is more of a problem that I bring up here:

    http://forums.microsoft.com/msdn/showpost.aspx?postid=1739625&siteid=1

     

    So I guess my question is... am I missing some way to tell the ContentControl: "this is what to use as the criterion for choosing a DataTemplate with ContentTemplateSelector"... and "this is the DataContext to use for chosen DataTemplate".

    If I could specify both of these, I think I'd be better off.

     

    Answers/Comments?

    Buzz

     

     

     

Answers

  • Monday, July 09, 2007 6:16 PMKevin Moore - MSFT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    You've certainly found a feature hole in our system.

     

    You're multi-binding solution is quite clever and may be your best option. At his time, we don't have a good way to enable what you want, but I will certainly take this problem as feedback for future work.

     

    Sorry for your trouble. Thanks for the great scenario.

All Replies

  • Monday, July 09, 2007 6:16 PMKevin Moore - MSFT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    You've certainly found a feature hole in our system.

     

    You're multi-binding solution is quite clever and may be your best option. At his time, we don't have a good way to enable what you want, but I will certainly take this problem as feedback for future work.

     

    Sorry for your trouble. Thanks for the great scenario.