views:

36

answers:

1

In my Master/detail scenario, I have a datagrid as master and a UserControl as child. When a particular Row being selected in master grid I assign it to the UserControl's DataContext. In my User Control I have couple of DataTemplates as Resources and ContentControl as the Child Container. What I'm struggling with is how to dynamically select the ContentTemplate of ContentControl based upon the UserControl's DataContext's property TemplateName.

<ContentControl ContentTemplate="{Binding Source={StaticResource ????}}" Content="{Binding}"/>
+1  A: 

You need to use DataTemplateSelector class. Read more here

UPDATE

In your case you should also check ContentTemplateSelector MSDN Article

You have to inherit from DataTemplateSelector, override and implement method SelectTemplate.

Then in XAML declare it as resource with specified key like this:

<DataTemplateSelectors:YourTemplateSelector x:Key="yourTemplateSelector"/>

Then in your ContentControl use it like this:

<ContentControl ContentTemplateSelector="{StaticResource yourTemplateSelector}" />
Eugene Cheverda
I have used the following code as my control is COntentControl and I cannot use the ItemTemplateSelector property in it and it still does not work.public class MyDataTemplateSelector:ContentControl{protected override void OnContentChanged(object oldContent, object newContent){ base.OnContentChanged(oldContent, newContent); if (newContent != null) { Product p = newContent as Product; if (p != null) { ContentTemplate = this.Resources["Product1"] as DataTemplate; } } }Any idea what's wrong
Jhelumi786
The idea is in TemplateSelector. Each entity linked with datatemplates has a TemplateSelector, for example, for ItemsControl it is ItemTemplateSelector, in your case it is a ContentTemplateSelector.Link below provide full info on it.http://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.contenttemplateselector.aspx
Eugene Cheverda
Well the ContentControl does not have the property called ContentTemplateSelector. When I set that Property, I get the error invalid property.
Jhelumi786
THis is what I get in output windowSystem.Windows.Data Error: 'MS.Internal.Data.DynamicValueConverter' converter failed to convert value 'Project.MyDataTemplateSelector' (type 'Project.MyDataTemplateSelector'); BindingExpression: Path='' DataItem='Project.MyDataTemplateSelector' (HashCode=24111608); target element is 'System.Windows.Controls.ContentControl' (Name='TicketView'); target property is 'ContentTemplate' (type 'System.Windows.DataTemplate').. System.InvalidOperationException: Can't convert type Project.MyDataTemplateSelector to type System.Windows.DataTemplate.
Jhelumi786
You're may be doing something wrong, because if you use really ContentControl class it HAS ContentTemplateSelector property, which marked with BindableProperty attribute.
Eugene Cheverda
MyDataTemplateSelector must bee inherited from DataTemplateSelector.
Eugene Cheverda
I belive that this Property is available in WPF and not in Silverlight
Jhelumi786
Wait some time, I'll try to implement and post to you sources.
Eugene Cheverda
thanx. I'm using SL4
Jhelumi786
http://www.codeproject.com/KB/silverlight/SLTemplateSelector.aspxCheck this link. I have replicated this and it works. You need to refine it for your needs. Properties (BrazilTemplate, UsaTemplate) you can obtain from resources by using TryFindResource. Identify what template to use by using either item or containter parameters.
Eugene Cheverda
Thanks Eugene. I 'll have a look into this and come back to U.In the meantime, I wonder if you could help me in another issue.As I said that this usercontrol is embebded on a page who has the datagrid. In this user control I'm displaying the dataForm. When I edit an item in this control in DataForm and save it, the changes reflect in the datagrid but my datagrid gets frozen and I cannot move to different pages. my datapager becomes disable as well.Any idea what's going wrong there
Jhelumi786
You have to trace all chain of runtime after saving edited item and figure out where it frozes your datagrid. It's unclear for me now to understand. As I may suppose your project is commercial, binded to your own database and you can't give me any source code, am I right?
Eugene Cheverda
I'm afraid that I can't give U the code.BUT I think this is just a simple scnario of Master Details.I just found that I cannot access the DataTemplates in the code behind file which are defined in the UserContorl.Resources section locally. I have to move them in the Resource dictionary of either external file or App.xaml Resources section. Which took me back to the Point 0 because I cannot use the code behind for them.On Main Page I want to have a datagrid as Master.and then I need to customise the Details section based upon the Item Type selected in the grid and write custom code4them
Jhelumi786