WinUI 3 Data Grid Having Different Formatting For Each Cell Using the DataTemplateSelector Class

I’m trying to create a simple data grid in WinUI 3 that uses custom DataTemplate that vary for the second column, creating different formatting for the second column’s cells depending on the data type of the cell’s data. A boolean would have a CheckBox and a string would have a TextBox.

I’m extending the DataTemplateSelector class to achieve this, per this page: https://learn.microsoft.com/en-us/windows/apps/design/controls/data-template-selector

The problem is, my extension’s override DataTemplateSelectorExt.SelectTemplateCore() method shown below doesn’t even run, so I can’t even test if the template code is good. There are no errors, but the second column of the data grid only shows the namespace of the CustomInfo class: ...ViewModels.CustomInfo.

My first question is, what is preventing SelectTemplateCore() from running and providing the second column with a custom DataTemplate?

XAML

   <Page.Resources>
        <local:DataTemplateSelectorExt
            x:Key="templateselctor"
            CheckBoxTemplate="{StaticResource CheckBoxTemplateResource}"
            TextBlockTemplate="{StaticResource TextBlockTemplateResource}"
            TextBoxTemplate="{StaticResource TextBoxTemplateResource}" />

    <DataTemplate x:Key="TextBlockTemplateResource" x:DataType="x:String">
        <Grid>
            <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" />
        </Grid>
    </DataTemplate>

    <DataTemplate x:Key="TextBoxTemplateResource" x:DataType="x:String">
        <Grid>
            <TextBox Text="{x:Bind}" />
        </Grid>
    </DataTemplate>

    <DataTemplate x:Key="CheckBoxTemplateResource" x:DataType="x:Boolean">
        <Grid>
            <CheckBox HorizontalAlignment="Center" VerticalAlignment="Center" IsChecked="{x:Bind}" />
        </Grid>
    </DataTemplate>

</Page.Resources>

<Grid>
    <DataGrid
        x:Name="dataGrid1"
        ItemsSource="{x:Bind ViewModel.CustomInfoItems}" >
        <DataGrid.Columns>
            <GridTextColumn
                MappingName="CFieldDisplayName" />
            <GridTemplateColumn
                CellTemplateSelector="{StaticResource templateselctor}"
                MappingName="CFieldValue" />
        </DataGrid.Columns>
    </DataGrid>
</Grid>

Code Behind

public sealed partial class MyPage : Page
{
    public DataGridViewModel ViewModel { get; }

    public MyPage()
    {
        this.InitializeComponent();

        ViewModel = new DataGridViewModel();
    }
}


public class DataTemplateSelectorExt : DataTemplateSelector
{
    public DataTemplate TextBlockTemplate { get; set; }
    public DataTemplate TextBoxTemplate { get; set; }
    public DataTemplate CheckBoxTemplate { get; set; }

    protected override DataTemplate SelectTemplateCore(object item) 
    { //This line is never his with a breakpoint
        if (item == null)
        {
            return TextBlockTemplate;
        }
        else if (item.GetType() == typeof(string))
        {
            return TextBoxTemplate;
        }
        else if (item.GetType() == typeof(bool))
        {
            return CheckBoxTemplate;
        }
        else
        {
            return TextBoxTemplate;
        }
    }
}

ViewModel

    public DataGridViewModel()
    {
        CustomInfoItems = new ObservableCollection<CustomInfo>();
        this.GenerateCutomInfoItems();
    }

    private void GenerateCutomInfoItems()
    {
        CustomInfoItems.Add(new CustomInfo() { CFieldDisplayName = "Custom TextBox Item", CFieldValue = "String Value" });
        CustomInfoItems.Add(new CustomInfo() { CFieldDisplayName = "Custom CheckBox Item", CFieldValue = true });
    }

public class CustomInfo
{
    public string CFieldDisplayName { get; set; }
    public object CFieldValue { get; set; }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật