Datatemplate with datatype not working?

Panagiotis Zografakis 21 Reputation points
2022-07-14T11:37:15.33+00:00
<Window xmlns="https://github.com/avaloniaui"  
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
        xmlns:vm="using:AvaloniaPlayground.ViewModels"  
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
        mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"  
        x:Class="AvaloniaPlayground.Views.MainWindow"  
		xmlns:mod ="using:AvaloniaPlayground.Models"  
		xmlns:mwvm="using:AvaloniaPlaygroundViewModels.MainWindowViewModel"  
        Icon="/Assets/avalonia-logo.ico"  
        Title="AvaloniaPlayground"  
		DataContext="vm:MainWindowViewModel">  
  
		<Window.ContentTemplate>  
			<DataTemplate DataType="{x:Type mod:model}">  
				<Grid RowDefinitions="50,50,50,50" ColumnDefinitions="200,200">  
					<TextBlock Grid.Row="0" Grid.Column="0">Id</TextBlock>  
					<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Id}"></TextBlock>  
					<TextBlock Grid.Row="1" Grid.Column="0">Name</TextBlock>  
					<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Name}"></TextBlock>  
				</Grid>  
			</DataTemplate>  
		</Window.ContentTemplate>  
  
  
	<Panel>  
		<ContentControl Content="{Binding Path=Model}"/>  
	</Panel>  
  
</Window>  
  
  
  
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
  
namespace AvaloniaPlayground.Models  
{  
        public class model  
        {  
                public int Id { get; set; }  
                public string Name { get; set; }  
        }  
}  
  
  
using ReactiveUI;  
using System;  
using System.Collections.Generic;  
using System.Text;  
  
namespace AvaloniaPlayground.ViewModels  
{  
        public class MainWindowViewModel : ViewModelBase  
        {  
                public string Greeting => "Welcome to Avalonia!";  
                private Models.model model;  
                public Models.model Model  
                {  
                        get => model;  
                        set  
                        {  
                                this.RaiseAndSetIfChanged(ref model, value);  
                        }  
                }  
  
                public MainWindowViewModel()  
                {  
                          
                        Model = new Models.model() { Id=5, Name="John"};  
                          
                          
                }  
        }  
}  

This code displays the ToString() method of the object. It seems to be failing to recognize that Model is of type Models.model. What's wrong?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,635 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 26,276 Reputation points Microsoft Vendor
    2022-07-15T06:31:59.027+00:00

    Hi,@Panagiotis Zografakis . Welcome Microsoft Q&A.
    For displaying data via Datatemplate with datatype, you could refer to the code below.

    Xaml:

    <Window.DataContext>  
        <local:MainWindowViewModel/>  
    </Window.DataContext>  
    <Window.Resources>  
        <DataTemplate  DataType="{x:Type local:model}">  
            <Grid>  
                <Grid.ColumnDefinitions>  
                    <ColumnDefinition Width="200"/>  
                    <ColumnDefinition Width="200"/>  
                </Grid.ColumnDefinitions>  
                <Grid.RowDefinitions>  
                    <RowDefinition Height="50"/>  
                    <RowDefinition Height="50"/>  
                </Grid.RowDefinitions>  
                <TextBlock Grid.Row="0" Grid.Column="0">Id</TextBlock>  
                <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Id}"></TextBlock>  
                <TextBlock Grid.Row="1" Grid.Column="0">Name</TextBlock>  
                <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Name}"></TextBlock>  
            </Grid>  
        </DataTemplate>  
    </Window.Resources>  
    <StackPanel>  
        <ContentControl Width="400" Content="{Binding Path=Model}"  />  
    
    </StackPanel>   
    

    Codebehind:
    220968-42.txt
    The result:
    221031-image.png


    If the response is helpful, please click "Accept Answer" and upvote it.
     Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread. 

    [5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html

    0 comments No comments

0 additional answers

Sort by: Most helpful