xamluwp-xamlwinui-3winuiwinui-xaml

How to specify a nested class for x:DataType in a DataTemplate?


When I try to use x:DataType in a DataTemplate to specify the nested class, I get an error.

I have a nested class structure in C#, as shown below:

namespace sample
{
    public class MainData
    {
        public class SubData
        {
        }
    }
}

XAML

xmlns:local="using:sample"

<ListView>
    <ListView.ItemTemplate>
        <DataTemplate x:DataType="local:MainData.SubData">
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

This results in a compile-time error because MainData.SubData is not recognized as a valid type.

Is there a way to specify a nested class like MainData.SubData for x:DataType in XAML?

Thank you in advance for your help!


Solution

  • Try the following syntax with + instead of ..

    <ListView>
        <ListView.ItemTemplate>
            <DataTemplate x:DataType="local:MainData+SubData">
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    

    At the moment?, Visual Studio will throw some errors but the app should run. Check this issue on GitHub.