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

DataTemplateSelector.SelectTemplateCore is passed a different "container" parameter on Desktop vs. WinwodwsAppSDK, Wasm, Android #16589

Open
christianfo opened this issue May 6, 2024 · 0 comments
Labels
area/skia ✏️ Categorizes an issue or PR as relevant to Skia difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. kind/bug Something isn't working project/layout 🧱 Categorizes an issue or PR as relevant to layouting and containers (Measure/Arrange, Collections,..)

Comments

@christianfo
Copy link

Current behavior

I have a ContentControl which utilizes a DataTemplateSelector to select one of several templates depending on the type of content (ViewModel) it is bound to. On WindowsAppSKD, Android, and Wasm (and presumably iOS though I have not tested) DataTemplateSelector.SelectTemplateCore is passed the ContentControl as the container. On Desktop, that method is passed the ContentPresenter used by the ContentControl as a container.

Expected behavior

All platforms to behave the same, and the same as WindowsAppSDK.

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

Minimal repro:
UnoApp25.zip

In this app, MainPage contains two instances of MyContentControl with their Content bound to different types of MyViewModels. MyContentControl uses a different template for each MyViewModel type, selected by a DataTemplateSelector. The commentary in DataTemplateSelector.SelectTemplateCore explains the differences in the incoming "container" parameter on different platforms, and shows the necessary platform dependent code:

    protected override DataTemplate? SelectTemplateCore(object item, DependencyObject container)
    {
        if (item != null && item is MyViewModel vm)
        {
#if !DESKTOP
            // On WindowsAppSDK, Wasm or Android, "container" is the ContentControl.
            var cc = container as MyContentControl;
#else
            // On Desktop, "container" is the ContentPresenter of the ContentControl.
            var cc = (container is ContentPresenter cp) ? cp.TemplatedParent as MyContentControl : null;
#endif

            if (cc != null)
            {
                return (DataTemplate)cc.Resources[vm.DataTemplateName];
            }

            Debug.Assert(false, "Unexpected container type in SelectTemplateCore");
        }
        return null;
    }

Workaround

The platform dependent code shows above does the trick. Nonetheless, the different platforms should all behave the same.

Works on UWP/WinUI

None

Environment

No response

NuGet package version(s)

{
"msbuild-sdks": {
"Uno.Sdk": "5.2.108"
}
}

Affected platforms

No response

IDE

Visual Studio 2022

IDE version

17.9.3

Relevant plugins

No response

Anything else we need to know?

Affected platforms: Desktop

@christianfo christianfo 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 May 6, 2024
@christianfo christianfo changed the title Scrolling a combobox with touch swipes keeps resetting the scrolling position. DataTemplateSelector.SelectTemplateCore is passed a different "container" parameter on Desktop vs. WinwodwsAppSDK, Wasm, Android May 6, 2024
@Youssef1313 Youssef1313 added the area/skia ✏️ Categorizes an issue or PR as relevant to Skia label May 6, 2024
@jeromelaban jeromelaban added project/layout 🧱 Categorizes an issue or PR as relevant to layouting and containers (Measure/Arrange, Collections,..) and removed triage/untriaged Indicates an issue requires triaging or verification labels May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/skia ✏️ Categorizes an issue or PR as relevant to Skia difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. kind/bug Something isn't working project/layout 🧱 Categorizes an issue or PR as relevant to layouting and containers (Measure/Arrange, Collections,..)
Projects
None yet
Development

No branches or pull requests

3 participants