Dynamic template selection

I am using Prism, and loading UI components from modules. I have the view models appearing correctly using the DockingAdapter, but I need to apply DataTemplates based upon the type of view model. I cannot hard code them in Xaml in the application's resources, so I need a DataTemplateSelector to select the proper template for the view model.


How can I use a DataTemplateSelector for the content in the DockingManager?


1 Reply

DR Durga Rajan Syncfusion Team February 27, 2024 08:34 PM UTC

Hi Russ,


Sorry for delay in getting back to you.


We understand that you wanted to have Template support in DockingAdapter. Here, we have attached a MVVM DockingAdapter sample which uses the new TemplateSelector property in DockingAdapter to provide DataTemplate for each item.


Code snippets :


public DataTemplateSelector ItemTemplateSelector

{

    get { return (DataTemplateSelector)GetValue(ItemTemplateSelectorProperty); }

    set { SetValue(ItemTemplateSelectorProperty, value); }

}

 

// Using a DependencyProperty as the backing store for ItemTemplateSelector.  This enables animation, styling, binding, etc...

public static readonly DependencyProperty ItemTemplateSelectorProperty =

    DependencyProperty.Register("ItemTemplateSelector", typeof(DataTemplateSelector), typeof(DockingAdapter), new PropertyMetadata(null));

 



<mvvm:DockingAdapter

    Grid.Row="1"

    ActiveDocument="{Binding ActiveDocument, Mode=TwoWay}"

    ItemTemplateSelector="{StaticResource AdapterDataTemplateSelector}"

    ItemsSource="{Binding Workspaces}" />


public class AdapterDataTemplateSelector : DataTemplateSelector

{

    public override DataTemplate SelectTemplate(object item, DependencyObject container)

    {

        // create dynamic datatemplate here

 

        if (item is Document) return (DataTemplate) Application.Current.Resources["DocumentView"];

        if (item is DocumentsViewModel) return (DataTemplate)Application.Current.Resources["DocumentsView"];

        if (item is PropertiesViewModel) return (DataTemplate)Application.Current.Resources["PropertiesView"];

        return base.SelectTemplate(item, container);

    }

}


  • Resources are defined in App.xaml.


This can be easily added to your PRISM sample. Please let us know if this addresses your needs or if we have misunderstood anything.


Regards,

Durga


Attachment: DockingDemo_7a8a7442.zip

Loader.
Up arrow icon