10

Given I have a GridView and I want to navigate to a different page by clicking each item.

How can navigate to a view associated to the viewmodel?

In WPF there is a way to set multiple Datatemplates for the viewmodel.

<TabControl Grid.Row="1" Margin="0" ItemsSource="{Binding Tabs}" SelectedIndex="0" SelectedItem="{Binding SelectedTab}">
    <TabControl.Resources>
        <DataTemplate DataType="{x:Type dashboard:DashboardViewModel}">
            <dashboard:DashboardView/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type controls:ExchangeViewModel}">
            <controls:ExchangeView/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type request:RequestViewModel}">
            <request:RequestView/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type addresses:AddressViewModel}">
            <addresses:AddressView/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type settings:ExchangeSettingsViewModel}">
            <settings:ExchangeSettingsView/>
        </DataTemplate>

    </TabControl.Resources>
    <TabControl.ItemTemplate>
        <DataTemplate DataType="vm:ViewModelBase">
            <TextBlock Text="{Binding Header}" FontSize="14"></TextBlock>
        </DataTemplate>
    </TabControl.ItemTemplate>
</TabControl>

This is what I tried in UWP in my particular case:

<Frame Grid.Row="1" DataContext="{x:Bind ViewModel.Value}">
    <Frame.Resources>
        <DataTemplate x:DataType="viewModels:ExampleViewModel1">
            <views:ExampleView1></views:ExampleView1>
        </DataTemplate>
        <DataTemplate x:DataType="viewModels:ExampleViewModel2">
            <views:ExampleView2></views:ExampleView2>
        </DataTemplate>
    </Frame.Resources>
</Frame>

The Frame is part of a page and I want to show the corresponding view based on the Value of the ViewModel.

Visual Studio tells me DataTemplate has to have a key attribute, but even then it doesn't work as it would in WPF, since it's not creating the view.

I know DataType was replaced with x:DataType and x:Type seems to be gone. Is there a way to achieve similar results?

Neil Turner
  • 2,712
  • 2
  • 18
  • 37
Andre
  • 1,228
  • 11
  • 24
  • 1
    Have you considered using a `ContentPresenter` to display your View Model? – Mike Eason Oct 21 '15 at 07:07
  • I'm going to add more information about the sitution I have in UWP, because the TabControl is an example only. – Andre Oct 21 '15 at 07:14
  • There is a thread on UserVoice for this [link](https://wpdev.uservoice.com/forums/110705-universal-windows-platform/suggestions/18575131-add-implicit-datatemplates-to-uwp) – Portikus May 09 '17 at 10:17

1 Answers1

14

In WPF, the DataType is a dependency property which can be retrieved in runtime.

In UWP, the x:DataType is compile-time property, you cannot get the value in runtime.

I created a simple demo about how to map the datatype and data template in UWP through DataTemplateSelector.

DataTemplateSelector:

namespace UWPApp
{
    public class Template
    {
        public string DataType { get; set; } 

        public DataTemplate DataTemplate { get; set; }
    }

    public class TemplateCollection2 : System.Collections.ObjectModel.Collection<Template>
    {
    }

    public class MyDataTemplateSelector : DataTemplateSelector
    {
        public TemplateCollection2 Templates { get; set; }

        private IList<Template> _templateCache { get; set; }

        public MyDataTemplateSelector()
        {

        }

        private void InitTemplateCollection()
        {
            _templateCache = Templates.ToList();
        }

        protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
        {
            if (_templateCache == null)
            {
                InitTemplateCollection();
            }

            if(item != null)
            {
                var dataType = item.GetType().ToString();

                var match = _templateCache.Where(m => m.DataType == dataType).FirstOrDefault();

                if(match != null)
                {
                    return match.DataTemplate;
                }
            }

            return base.SelectTemplateCore(item, container);
        }
    }
}

ViewModel:

namespace UWPApp
{
    public class ViewModel1
    {
        public string Text1 { get; set; }
    }

    public class ViewModel2
    {
        public string Text2 { get; set; }
    }
}

XAML:

<Grid 
    x:Name="container"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.Resources>
        <local:TemplateCollection2 x:Key="templates">
            <local:Template DataType="UWPApp.ViewModel1">
                <local:Template.DataTemplate>
                    <DataTemplate x:DataType="local:ViewModel1">
                        <StackPanel>
                            <TextBlock Text="{Binding Text1}"></TextBlock>
                            <TextBlock Text="From template1"></TextBlock>
                        </StackPanel>
                    </DataTemplate>
                </local:Template.DataTemplate>
            </local:Template>
            <local:Template DataType="UWPApp.ViewModel2">
                <local:Template.DataTemplate>
                    <DataTemplate x:DataType="local:ViewModel2">
                        <StackPanel>
                            <TextBlock Text="{Binding Text2}"></TextBlock>
                            <TextBlock Text="From template2"></TextBlock>
                        </StackPanel>
                    </DataTemplate>
                </local:Template.DataTemplate>
            </local:Template>
        </local:TemplateCollection2>
       <local:MyDataTemplateSelector 
        x:Key="myDataTemplateSelector" Templates="{StaticResource templates}">
       </local:MyDataTemplateSelector>
    </Grid.Resources>
    <StackPanel>
        <Button x:Name="button" Click="button_Click">Click Me</Button>
        <ContentControl x:Name="stage" ContentTemplateSelector="{StaticResource myDataTemplateSelector}">

        </ContentControl>
    </StackPanel>
</Grid>
Neil Turner
  • 2,712
  • 2
  • 18
  • 37
Jeffrey Chen
  • 4,650
  • 1
  • 18
  • 22
  • does this actually work ? looks like in m.DataType == dataType the m.DataType will evaluate to 'UWPApp.ViewModel1' (jus string) and dataType to 'ViewModel1' so they won't match. Where do you actually set datacontext (ViewModles) for views ? – IronHide Oct 26 '21 at 12:04