Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

c# - CollectionView shuffling the Buttons added when I scroll the CollectionView

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...