1

更新:我通过支持渠道收到了 MS 的答复,当 ItemPanelTemplate 是 VariableSizeWrapGrid 时,目前无法支持触摸重新排序 - 因为它缺少实现所需的某些接口。

作为前言(为了消除任何误解),我确实具备使用鼠标所需的功能。我只需要通过触摸手势实现相同的功能。

要求: 1. 在可水平滚动的容器中显示一组项目,每个项目可以是半高或全高。应该可以通过在新位置拖放来重新排列它们。2. 为用户提供一种取消选择先前选择的项目的方法(与用鼠标右键单击的效果相同)。

我当前的代码:

XAML(为了清楚起见稍微简化了)

    <ScrollViewer HorizontalAlignment="Stretch" 
                  HorizontalScrollBarVisibility="Auto" 
                  VerticalScrollBarVisibility="Hidden">
        <StackPanel Orientation="Horizontal" Height="600" 
                    ScrollViewer.VerticalScrollMode="Disabled">
            <controls:MyGridView ScrollViewer.VerticalScrollMode="Disabled"
                                 ItemsSource="{Binding Items}" 
                                 SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
                                 SelectionMode="Single" 
                                 IsSwipeEnabled="True" 
                                 CanDragItems="True" 
                                 CanReorderItems="True" 
                                 AllowDrop="True" 
                                 IsItemClickEnabled="False"
                                 Height="600">
                <GridView.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <ContentControl Content="{Binding}" 
                                            ContentTemplateSelector="{StaticResource ItemTemplateSelector}"/>
                        </Grid>
                    </DataTemplate>
                </GridView.ItemTemplate>

                <GridView.ItemsPanel>
                    <ItemsPanelTemplate>
                        <VariableSizedWrapGrid ItemHeight="300" 
                                               ItemWidth="300" 
                                               Orientation="Vertical"/>
                    </ItemsPanelTemplate>
                </GridView.ItemsPanel>

                <GridView.ItemContainerStyle>
                    <Style TargetType="GridViewItem">
                        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                        <Setter Property="VerticalContentAlignment" Value="Stretch"/>
                    </Style>
                </GridView.ItemContainerStyle>
            </controls:DashletsGridView>
        </StackPanel>
    </ScrollViewer>

这里的 MyGridView 类是 GridViewEx 的扩展,它是本示例的一部分。它对其基类的唯一补充是:

    protected override void PrepareContainerForItemOverride(Windows.UI.Xaml.DependencyObject element, object item)
    {
        try
        {
            Item d = item as Item;
            if (d != null)
            {
                element.SetValue(VariableSizedWrapGrid.RowSpanProperty, d.Size == PossibleSizes.Half ? 1 : 2);
            }
        }
        catch
        {
            element.SetValue(VariableSizedWrapGrid.RowSpanProperty, 2);
        }
        finally
        {
            base.PrepareContainerForItemOverride(element, item);
        }
    }

我尝试了各种各样的布局控件组合(例如消除外部的 ScrollViewer 或 StackPanel,或者用网格替换它),但没有一个支持正确的水平滚动或项目大小。将 GridView.ItemsPanel 从 VariableSizedWrapGrid 切换到 WrapGrid 也无济于事。

请注意标志的组合:IsSwipeEnabled="True" CanDragItems="True" CanReorderItems="True" AllowDrop="True" IsItemClickEnabled="False"。这就是鼠标 d&d 的工作原理,但触摸 d&d 或取消选择仍然没有运气。

4

1 回答 1

0

GridView除非aItemsSource绑定到ObservableCollectionand CanReorderItems,否则不能重新排序 a CanDragItems,并且AllowDrop设置为true不必使用 a来CollectionViewSource启用您的gridview. 实际上,acollectionviewsource经常用于对 a 进行分组,gridview并且在对数据进行分组时无法重新排序。

无论如何,您的 XAML 将如下所示:

<Grid Background="Black">
    <Grid.DataContext>
        <local:MyModel/>
    </Grid.DataContext>
    <GridView CanReorderItems="True" CanDragItems="True" AllowDrop="True"
              ItemsSource="{Binding Items}">
    </GridView>
</Grid>

尽管 anyenumerable可以绑定到ItemsSourcea GridView,但它只有一个ObservableCollection可以重新排序。是的,您可以使用实现重新排序的自定义类型,但为什么要在ObservableCollection什么时候为您解决这个问题呢?

这是我在这里回答的一个片段。

不要气馁。它比看起来容易。

祝你好运!

于 2013-06-08T05:11:17.607 回答