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

The bug of DataTemplateSelector in ListView #17461

Open
lindexi opened this issue Jul 11, 2024 · 2 comments
Open

The bug of DataTemplateSelector in ListView #17461

lindexi opened this issue Jul 11, 2024 · 2 comments
Assignees
Labels
area/listview 📃 Categorizes an issue or PR as relevant to the ListView control area/skia ✏️ Categorizes an issue or PR as relevant to Skia 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

@lindexi
Copy link
Contributor

lindexi commented Jul 11, 2024

Current behavior

This problem is difficult to describe in words, although it is a very simple problem to reproduce.

The data on the list is messed up.

Expected behavior

The correspondence between the data in a row is correct.

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

  1. Create an empty Uno project and add the ListView to xaml.
  2. Add the ContentPresenter in DataTemplate of the ListView. And then set the ContentTemplateSelector to ContentPresenter with FooDataTemplateSelector
  3. The code of FooDataTemplateSelector is:
public class FooDataTemplateSelector : DataTemplateSelector
{
    protected override DataTemplate SelectTemplateCore(object item)
    {
        UIElement view = new ReadonlyValueEditorView(item);

#if HAS_UNO
        return new DataTemplate(() => view);
#else
        return null!;
#endif
    }
}

public class ReadonlyValueEditorView : UserControl
{
    public ReadonlyValueEditorView(object initialValue)
    {
        this.DataContext(
            new BindableObjectEditorModel()
                .WithModel(x => x.Model.Value = initialValue),

            (v, vm) => v
                .Content(new TextBlock()
                    .Text(x => x.Binding(() => vm.Value))
                ));
    }
}

public partial record ObjectEditorModel
{
    public object? Value { get; set; }
}

internal static class EditorViewExtensions
{
    public static TBindable WithModel<TBindable>(this TBindable bindableModel, Action<TBindable> setter)
        where TBindable : BindableViewModelBase
    {
        setter(bindableModel);
        return bindableModel;
    }
}

Running the project with net8.0-desktop target framework, and then you can find the data on the list is messed up.

All the MainPage.xaml code:

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

  <Grid>
    <ListView x:Name="FooListView">
      <ListView.ItemTemplate>
        <DataTemplate x:DataType="local:DataModel">
          <Grid  ColumnDefinitions="*,*">
            <TextBlock Grid.Column="0" Text="{x:Bind Value}"/>
            <ContentPresenter Grid.Column="1" Content="{x:Bind}"
                              ContentTemplateSelector="{StaticResource FooDataTemplateSelector}" />
          </Grid>
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>
  </Grid>
</Page>

All the MainPage.xaml.cs code:

using Uno.Extensions.Reactive.Bindings;

namespace CadeqaciwhiYibiruhache;

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();

        var list = new List<DataModel>();
        for (int i = 0; i < 100; i++)
        {
            list.Add(new DataModel(i.ToString()));
        }

        FooListView.ItemsSource = list;
    }
}


public class FooDataTemplateSelector : DataTemplateSelector
{
    protected override DataTemplate SelectTemplateCore(object item)
    {
        UIElement view = new ReadonlyValueEditorView(item);

#if HAS_UNO
        return new DataTemplate(() => view);
#else
        return null!;
#endif
    }
}

public class ReadonlyValueEditorView : UserControl
{
    public ReadonlyValueEditorView(object initialValue)
    {
        this.DataContext(
            new BindableObjectEditorModel()
                .WithModel(x => x.Model.Value = initialValue),

            (v, vm) => v
                .Content(new TextBlock()
                    .Text(x => x.Binding(() => vm.Value))
                ));
    }
}

public partial record ObjectEditorModel
{
    public object? Value { get; set; }
}

internal static class EditorViewExtensions
{
    public static TBindable WithModel<TBindable>(this TBindable bindableModel, Action<TBindable> setter)
        where TBindable : BindableViewModelBase
    {
        setter(bindableModel);
        return bindableModel;
    }
}


public record DataModel(string Value);

You can find my repro demo code in https://github.com/lindexi/lindexi_gd/tree/3271e3dbebfcf85e04f1c329ca3a802cdcbee87b/UnoDemo/CadeqaciwhiYibiruhache

Workaround

No response

Works on UWP/WinUI

None

Environment

No response

NuGet package version(s)

global.json:

{
  // To update the version of Uno please update the version of the Uno.Sdk here. See https://aka.platform.uno/upgrade-uno-packages for more information.
  "msbuild-sdks": 
  {
    "Uno.Sdk": "5.2.175"
  },
  "sdk": 
  {
    "allowPrerelease": false
  }
}

Affected platforms

No response

IDE

No response

IDE version

No response

Relevant plugins

No response

Anything else we need to know?

No response

@lindexi lindexi 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 Jul 11, 2024
@MartinZikmund MartinZikmund added area/skia ✏️ Categorizes an issue or PR as relevant to Skia area/listview 📃 Categorizes an issue or PR as relevant to the ListView control project/items 🧾 Categorizes an issue or PR as relevant to items (ItemsControl, ItemsRepeater, ...) 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 Jul 16, 2024
@Youssef1313 Youssef1313 self-assigned this Aug 6, 2024
@jeromelaban
Copy link
Member

@lindexi would you know if this still happens in the latest builds?

@jeromelaban jeromelaban added the triage/needs-information Indicates an issue needs more information in order to work on it. label Nov 18, 2024
lindexi added a commit to lindexi/lindexi_gd that referenced this issue Nov 19, 2024
@lindexi
Copy link
Contributor Author

lindexi commented Nov 19, 2024

@jeromelaban Yes, we can find this issues in 5.5.43 version.

@github-actions github-actions bot removed the triage/needs-information Indicates an issue needs more information in order to work on it. label Nov 19, 2024
@MartinZikmund MartinZikmund added the difficulty/medium 🤔 Categorizes an issue for which the difficulty level is reachable with a good understanding of WinUI label Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/listview 📃 Categorizes an issue or PR as relevant to the ListView control area/skia ✏️ Categorizes an issue or PR as relevant to Skia 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

5 participants