English 中文(简体)
UWP - the order will be wrong on GridView when the column counts be changed
原标题:

I encounter a problem that the order will be wrong on Gridview when the column count be changed. Could someone help to solve, please? enter image description here

MainPage.xaml

<Page
    x:Class="DragDropTestApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="using:DragDropTestApp"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
    mc:Ignorable="d">

    <Grid>

        <Button Click="Button_Click" Content="Update" />
        <GridView
            x:Name="grid"
            Margin="100,10,0,10"
            AllowDrop="True"
            CanDragItems="True"
            CanReorderItems="True"
            IsSwipeEnabled="True"
            ItemsSource="{x:Bind ViewModel.PreviewItems}">
            <GridView.ItemTemplate>
                <DataTemplate>
                    <Grid
                        Width="340"
                        Height="240"
                        Padding="5,0,5,0"
                        BorderThickness="1">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="50" />
                            <RowDefinition Height="180" />
                        </Grid.RowDefinitions>

                        <ContentPresenter
                            Grid.Row="1"
                            HorizontalAlignment="Left"
                            VerticalAlignment="Top"
                            Content="{Binding ImageThumbnail}" />
                    </Grid>

                </DataTemplate>
            </GridView.ItemTemplate>
            <GridView.ItemsPanel>
                <ItemsPanelTemplate x:Name="clu">
                    <WrapGrid MaximumRowsOrColumns="5" Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </GridView.ItemsPanel>
        </GridView>
    </Grid>
</Page>

MainViewModel.cs

public class MainViewModel : ObservableObject
    {
        public ObservableCollection<PreviewItem> PreviewItems = new ObservableCollection<PreviewItem>();
        public MainViewModel()
        {
            for (int i = 0; i < 20; i++)
            {
                var item = new PreviewItem();
                item.ImageThumbnail = new Windows.UI.Xaml.Controls.Image();
                ImageSource result = new BitmapImage(new Uri($"ms-appx:///Assets/{i + 1}.png"));
                item.ImageThumbnail.Source = result;

                PreviewItems.Add(item);
            }
        }
    }

PreviewItem.cs

public class PreviewItem : ObservableObject
{
    private Image _imageThumbnail;
    public Image ImageThumbnail { get => _imageThumbnail; set => SetProperty(ref _imageThumbnail, value); }
  

    public PreviewItem()
    {
        
    }     
   
}

Here is my project and cord. https://github.com/houzhiwei/UWPProject

Thanks

Zack

I develop a page list on gridview and hope it work normal.

问题回答

This is caused by the GridView s default virtualization behavior, which reuses the containers of the items when the item is scrolled out of view.

If you don t want this behavior, it is recommended that you replace WrapGrid with ItemsWrapGrid and set CacheLength="0" to disable GridView virtualization.

<GridView.ItemsPanel>
    <ItemsPanelTemplate x:Name="clu">
        <ItemsWrapGrid CacheLength="0" MaximumRowsOrColumns="5" Orientation="Horizontal" />
    </ItemsPanelTemplate>
</GridView.ItemsPanel>




相关问题
asp.net gridview editindex

Please take a look at this: protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { int index = Convert.ToInt32(e.CommandArgument); if (e.CommandName == "Edit") {...

How do I stop my gridview from caching?

I search for results, click one, it opens in a new window, then edit it, and close the popup. Then I hit search to refresh my gridview, but the changes do not reflect unless I hit F5. It s caching ...

get data from gridview without querying database

I am new at this so please bear with me... I have managed to get the following code to work...so when I click on the "select" link in each row of the gridview, the data is transfered to other label/...

WPF GridView, Display GetType().Name in DisplayMemberBinding

I want include the Type name of each object in my collection from my GridView. I have a collection which has four different types in it that all derive from a base class. Call them, Foo, Bar, Fizz, ...

GridView that toggles percentages of counts

I am using a SqlDataSource that returns a table of raw counts. One of these columns is the "total". I would like to give the user the ability to show these counts as a percentage of total using some ...

热门标签