简体   繁体   English

UWP Gridview 固定项目位置

[英]UWP Gridview fixed item position

In my UWP app, I have this gridview with items reordering enabled在我的 UWP 应用程序中,我启用了此网格视图并启用了项目重新排序

        <GridView Name="ModulesGridView" CanReorderItems="True" ItemsSource="{x:Bind DataSource, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
 IsItemClickEnabled="False"  IsHoldingEnabled="False" IsTapEnabled="False" AllowDrop="True" Drop="Drop_Module"
 DragOver="Grid_DragOver" ItemTemplateSelector="{StaticResource MyDataTemplateSelector}">           
        </GridView>

and I want to have an item in this gridview to be always at the end, no matter how I will reorder the items.并且我希望这个 gridview 中有一个项目总是在最后,无论我如何重新排序项目。 It will be always at the end.它总是在最后。 Is there any way to achieve this?有没有办法实现这一目标?

GridView does not have a native API to support this feature, but we can achieve the goal in other ways. GridView没有原生 API 来支持此功能,但我们可以通过其他方式实现目标。

For simplicity, we assume that your DataSource is of type ObservableCollection<int> .为简单起见,我们假设您的DataSourceObservableCollection<int>类型。 If your actual type is more complex than int, you need to override the EqualTo method for this class in advance.如果您的实际类型比 int 更复杂,则需要提前覆盖EqualTo方法。

1. Allow CanDragItems and add corresponding events 1.允许CanDragItems并添加相应事件

<GridView ...
          CanDragItems="True"
          DragItemsCompleted="ModulesGridView_DragItemsCompleted">
</GridView>

2. Extract the last item in advance when loading the data collection 2.加载数据集合时提前提取最后一项

private int LastItem = 0;

...

for (int i = 0; i < 20; i++)
{
    DataSource.Add(i);
}
LastItem = DataSource.Last();

3. Check the position of the last item in the DragItemsCompleted event 3.检查DragItemsCompleted事件中最后一项的位置

private void ModulesGridView_DragItemsCompleted(ListViewBase sender, DragItemsCompletedEventArgs args)
{
    int index = DataSource.IndexOf(LastItem);
    if (index != DataSource.Count - 1)
    {
        DataSource.Move(index, DataSource.Count - 1);
    }
}

In other words, we do not interfere with the process of reordering items by the user, but after reordering, we detect the position of the last item.换句话说,我们不干扰用户重新排序项目的过程,而是在重新排序后检测最后一个项目的位置。 If it is not at the end, we move it again.如果它不在最后,我们再次移动它。

Thanks.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM