Quantcast
Channel: User Liyun Zhang - MSFT - Stack Overflow
Viewing all articles
Browse latest Browse all 444

Answer by Liyun Zhang - MSFT for .Net Maui CollectionView. Changes to one item's view properties affects wrong item. Recycling problem?

$
0
0

First, the TintColor="{Binding Source={x:Reference ThisComponent} and Label Text="{Binding Source={x:Reference Component} bind to different object. Are they correct?

And then I created a new sample to test your code and every thing worked well. I will show the main code I used.

The customview.xaml:

<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"             xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"             xmlns:local="clr-namespace:TestMaui"             x:Class="TestMaui.MyView"             x:Name="MyComponent"><ContentView.Resources><local:StateToColorConverter x:Key="myconverter"/></ContentView.Resources><VerticalStackLayout><Image          Source="dotnet_bot.png"          HeightRequest="185"          Aspect="AspectFit"          SemanticProperties.Description="dot net bot in a race car number eight" ><Image.Behaviors><toolkit:IconTintColorBehavior TintColor="{Binding Source={x:Reference MyComponent}, Path=MyItem.State, Converter= {x:StaticResource myconverter}}"/></Image.Behaviors></Image><Label Text="{Binding Source={x:Reference MyComponent}, Path=MyItem.State}"/></VerticalStackLayout></ContentView>

The customview.cs:

public partial class MyView : ContentView{    public static readonly BindableProperty MyItemProperty =BindableProperty.Create(nameof(MyItem),typeof(MyItem),typeof(MyView),default(Nullable));    public MyItem MyItem    {        get => (MyItem)GetValue(MyItemProperty);        set => SetValue(MyItemProperty, value);    }    public MyView()      {            InitializeComponent();      }}

And the other classes:

    public partial class MyItem : ObservableObject    {        [ObservableProperty]        private ItemColor state;    }    public enum ItemColor    {        Green,Blue,Red    }    public class StateToColorConverter : IValueConverter    {        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)        {            if (value == null) return null;            if((ItemColor)value == ItemColor.Green)            {                return Colors.Green;            }else if((ItemColor)value==ItemColor.Blue)            {                return Colors.Blue;            }else return Colors.Red;        }        public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)        {            throw new NotImplementedException();        }    }

Use them in the mainpage.xaml:

<CollectionView x:Name="collec"><CollectionView.ItemTemplate><DataTemplate x:DataType="{x:Type local:MyItem}"><local:MyView MyItem="{Binding .}"/></DataTemplate></CollectionView.ItemTemplate></CollectionView>

And the mainpage.cs:

 public ObservableCollection<MyItem> items;  public MainPage()  {      InitializeComponent();      items = new ObservableCollection<MyItem>();      items.Add(new MyItem() { State = ItemColor.Green });      items.Add(new MyItem() { State= ItemColor.Red });      items.Add(new MyItem() { State= ItemColor.Blue });      collec.ItemsSource = items;  }  private void OnCounterClicked(object sender, EventArgs e)  {      items[0].State = (ItemColor)((int)Math.Abs((int)(items[0].State - 2)));  }

Everything worked well.


Viewing all articles
Browse latest Browse all 444

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>