I have added buttons in CollectionView straring from B1 to B20 (Button names). But when i scroll the collectionView the buttons position is shuffling / mixing randomly.
c# code
_ButtonList = new List<Button>();
for (int i = 0; i < 20; i++)
{
_ButtonList.Add(new Button { Text =$"B{i}" });
}
_collectionView.ItemsLayout = new GridItemsLayout(1, ItemsLayoutOrientation.Vertical);
int ButtonIndex = 0;
DataTemplate _dataTemplate = new DataTemplate(() =>
{
Grid grid = new Grid();
grid.Padding = 0;
grid.RowDefinitions.Add(new RowDefinition { Height=GridLength.Auto});
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
Grid.SetRow(_ButtonList[ButtonIndex],0);
Grid.SetColumn(_ButtonList[ButtonIndex], 0);
grid.Children.Add(_ButtonList[ButtonIndex]);
ButtonIndex++;
return grid;
});
_collectionView.ItemTemplate = _dataTemplate;
_collectionView.ItemsSource = _ButtonList;
xaml code
<CollectionView x:Name="_collectionView" ItemsSource="{Binding _ButtonList}">
</CollectionView>
After page loaded (not scrolled) everything is ok
After scrolling positions are mixed
question from:
https://stackoverflow.com/questions/65901847/collectionview-shuffling-the-buttons-added-when-i-scroll-the-collectionview 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…