question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support generic types in DataTemplate.DataType

See original GitHub issue

E.g.:

<DataTemplate DataType="core:Library{core:ShapeStyle}">

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
grokyscommented, May 12, 2020

There’s no workaround for XAML currently, XAML in general doesn’t support generic types well, so I’m not entirely sure how to add this.

However, you should be able to add DataTemplates with generic types from code, if that would help?

0reactions
jpgarza93commented, Aug 8, 2023

For anyone trying to use generics (String, Double, Int, etc), here is what I did to get it working:

public class GenericsDataTemplate : IDataTemplate
{
    [DataType]
    public Type? DataType { get; set; }

    [Content]
    [TemplateContent]
    public object? Content { get; set; }

    public bool Match(object? data)
    {
        if (DataType == null)
            return true;

        if (data is Type type)
            return DataType == type;

        return false;
    }

    public Control? Build(object? data) => Build(data, null);

    public Control? Build(object? data, Control? existing)
    {
        return existing ?? TemplateContent.Load(Content)?.Result;
    }
}

then in xaml just do this image

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to reference a generic type in the DataType attribute ...
No, you cannot express a generics type in XAML. You will have to create a concrete type that extends your generic one ....
Read more >
DataTemplate with an object taking a generic
There's no direct support for hooking a DataTemplate to a generic type. However, the generally accepted way of working around this is to...
Read more >
generic type in xaml - Microsoft Q&A
Hi <DataTemplate DataType="{x:Type vm:MyViewModel1}"> <StackPanel ... any help will be appreciated, thanks in advance.
Read more >
Does Angular Support Generic Component Types?
We can't provide generic types to our template selectors so how does Angular support generic components? In this article we will explain how ......
Read more >
Avalonia: Support for TypeName property on x:Type in xaml ...
Avalonia: Support for TypeName property on x:Type in xaml editor. When setting TypeName property on <x:Type /> I get error Cannot resolve symbol...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found