Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Desktop] Unexpected null value passed to ItemTemplateSelector when used on Canvas #18693

Open
kucint opened this issue Nov 5, 2024 · 3 comments
Labels
difficulty/medium 🤔 Categorizes an issue for which the difficulty level is reachable with a good understanding of WinUI kind/bug Something isn't working project/items 🧾 Categorizes an issue or PR as relevant to items (ItemsControl, ItemsRepeater, ...)

Comments

@kucint
Copy link

kucint commented Nov 5, 2024

Current behavior

Create canvas that displays two types of objects ItemA and ItemB and define a ItemTemplateSelector with two DataTemplates for both item types:

<Page x:Class="UnoTemplateSelectorOnCanvasApp.MainPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="using:UnoTemplateSelectorOnCanvasApp"
      Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Page.Resources>

        <DataTemplate x:Key="ItemTemplateA">
            <Border Background="LightGreen" Width="50" Height="50"/>
        </DataTemplate>

        <DataTemplate x:Key="ItemTemplateB">
            <Border Background="LightBlue" Width="50" Height="50"/>
        </DataTemplate>

        <local:ItemTemplateSelector x:Key="ItemTemplateSelector"
                                    TemplateA="{StaticResource ItemTemplateA}"
                                    TemplateB="{StaticResource ItemTemplateB}"/>
    </Page.Resources>
    
    <ItemsControl x:Name="ItemPanel" ItemsSource="{x:Bind Items, Mode=OneWay}" ItemTemplateSelector="{StaticResource ItemTemplateSelector}" >
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas Background="LightYellow"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
</Page>

This works fine on WindowsSDK Framework:

image

But on Desktop Framework the ItemTemplateSelector unexpectedly gets null as an item reference where only ItemA and ItemB are expected.

internal sealed partial class ItemTemplateSelector : DataTemplateSelector
{
    public DataTemplate? TemplateA { get; set; }
    public DataTemplate? TemplateB { get; set; }

    protected override DataTemplate? SelectTemplateCore(object item, DependencyObject container) => item switch
    {
        ItemA => TemplateA,
        ItemB => TemplateB,

        // this should never happen: only ItemA and ItemB are expected to be passed to this selector
        // -> On WindowsSDK Framework: all is fine, null never happens
        // -> on Desktop Framework: null is passed to this selector (bug)
        //null => null, // uncomment this line for work-around

        // this should never happen
        _ => throw new InvalidOperationException($"Unrecognized view model {item.GetType()}"),
    };
}

Expected behavior

null shall never be passed to the ItemTemplateSelector

How to reproduce it (as minimally and precisely as possible)

STEPS TO REPRODUCE:

  • download the app
  • compile and run

MINIMAL REPRO PROJECT: UnoTemplateSelectorOnCanvasApp.zip

Workaround

handle null value in ItemTemplateSelector:
null => null,

Works on UWP/WinUI

Yes

Environment

No response

NuGet package version(s)

"Uno.Sdk": "5.5.32"

Affected platforms

Skia (WPF)

IDE

Visual Studio 2022

IDE version

No response

Relevant plugins

No response

Anything else we need to know?

No response

@kucint kucint added difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. kind/bug Something isn't working triage/untriaged Indicates an issue requires triaging or verification labels Nov 5, 2024
@jeromelaban
Copy link
Member

Thanks for the report.

This is a known behavior at this time, and also a good practice to react to null being passed as parameter, as this is a valid use case from items sources.

@kucint
Copy link
Author

kucint commented Nov 5, 2024

@jeromelaban
ok. thanks for info.

@MartinZikmund
Copy link
Member

@kucint you can safely return null in case of null parameter

@MartinZikmund MartinZikmund added project/items 🧾 Categorizes an issue or PR as relevant to items (ItemsControl, ItemsRepeater, ...) difficulty/medium 🤔 Categorizes an issue for which the difficulty level is reachable with a good understanding of WinUI and removed triage/untriaged Indicates an issue requires triaging or verification difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. labels Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty/medium 🤔 Categorizes an issue for which the difficulty level is reachable with a good understanding of WinUI kind/bug Something isn't working project/items 🧾 Categorizes an issue or PR as relevant to items (ItemsControl, ItemsRepeater, ...)
Projects
None yet
Development

No branches or pull requests

3 participants