【问题标题】:how to reorder an item in itemscontrol by holding in item?如何通过保留项目来重新排序项目控制中的项目?
【发布时间】:2015-10-07 15:01:03
【问题描述】:

您好,我尝试在 ItemsControl 中重新排序 items。当我将ItemsControl 中的元素ManipulationMode 设置为ManipulationModes.TranslateY ScrollView 时工作。对于这个问题,我在Holding 事件中更改了这个ManipulationMode,但我遇到了一个新问题。 在我的代码中,我们需要在按住后松开手指并再次触摸以进行移动。我需要按住和移动手指来拖动不按住和释放的项目,然后再次触摸和移动。这个版本对我和用户都不好 请帮帮我。

我的代码:

<ItemsControl ItemsSource="{Binding Items}"
                x:Name="todoList">

            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Border Background="{Binding Path=color, Converter={StaticResource ColorToBrushConverter}}"
                Height="75"
                x:Name="todoItem">
                        <i:Interaction.Behaviors>
                            <Behaviors:DragReOrderBehavior />
                        </i:Interaction.Behaviors>

                        <Grid Background="{StaticResource itemGradient}">
                             <!--task text-->
                            <TextBlock Text="{Binding Title}"
                       Margin="15,15,0,15" FontSize="25" TextWrapping="Wrap"
                       x:Name="taskText"/>
                        </Grid>
                    </Border>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

和行为:

 public class DragReOrderBehavior : DependencyObject, IBehavior
{
    private static readonly int AutoScrollHitRegionSize = 80;

    private DispatcherTimer _autoScrollTimer;
    private FrameworkElement _dragItem;
    private int _initialDragIndex;
    private bool IsActive = false;
    private ItemsControl itemscontrolList;
    private ItemViewModel draggedItemVM;
    private MainViewModel VM;
    private ResettableObservableCollection<ItemViewModel> itemsListVm;
    private ScrollViewer _scrollViewer;
    // private SoundEffect _moveSound;
    public DependencyObject AssociatedObject { get; private set; }


    public void Attach(DependencyObject associatedObject)
    {
        AssociatedObject = associatedObject;
        var fw = (FrameworkElement)AssociatedObject;

        //fw.ManipulationMode = ManipulationModes.TranslateY | ManipulationModes.System;
        ////fw.ManipulationStarted += fw_ManipulationStarted;
        fw.Holding += fw_Holding;
        //fw.ManipulationDelta += fw_ManipulationDelta;
        //fw.ManipulationCompleted += fw_ManipulationCompleted;
        if (fw.RenderTransform == null || fw.RenderTransform as TranslateTransform == null)
        {
            fw.RenderTransform = new TranslateTransform();
        }
        // a timer which is used to periodically detect the position of the
        // item being dragged in order to allow auto-scroll behaviour
        _autoScrollTimer = new DispatcherTimer();
        _autoScrollTimer.Interval = TimeSpan.FromMilliseconds(50);
        _autoScrollTimer.Tick += (s, e) =>
        {
            AutoScrollList();
            ShuffleItemsOnDrag();
        };

        // _moveSound = SoundEffect.FromStream(TitleContainer.OpenStream("Sounds/Windows XP Menu Command.wav"));
    }

    void fw_Holding(object sender, HoldingRoutedEventArgs e)
    {
        //if (Math.Abs(e.Cumulative.Translation.Y) > Math.Abs(e.Cumulative.Translation.X))
       // {
        //var fw = (FrameworkElement)AssociatedObject;

        //fw.ManipulationMode = ManipulationModes.TranslateRailsY | ManipulationModes.System;
            IsActive = true;

            // locate the element being dragged
            _dragItem = AssociatedObject as FrameworkElement;
            itemscontrolList = AssociatedObject.Ancestors<ItemsControl>().OfType<ItemsControl>().FirstOrDefault();
            draggedItemVM = ((ItemViewModel)_dragItem.DataContext);
            VM = (MainViewModel)itemscontrolList.DataContext;
            itemsListVm = VM.Items;
            _scrollViewer = itemscontrolList.Descendants<ScrollViewer>()
                              .Cast<ScrollViewer>()
                              .SingleOrDefault();
            _dragItem.SetVerticalOffset(0);
            _dragItem.ManipulationMode = ManipulationModes.TranslateY;
            _dragItem.ManipulationDelta += fw_ManipulationDelta;
            _dragItem.ManipulationCompleted += fw_ManipulationCompleted;
            _dragItem.UpdateLayout();
            itemscontrolList.UpdateLayout();

            _initialDragIndex = itemsListVm.IndexOf(draggedItemVM);

            // fade out the items in the list, other than the dragged one
            foreach (var item in itemscontrolList.GetItemsInView()
                                          .Where(i => i.DataContext != draggedItemVM))
            {
                item.Animate(1.0, 0.7, "Opacity", 300, 0);
            }

            _autoScrollTimer.Start();
       // }
    }

}

【问题讨论】:

    标签: xaml drag-and-drop windows-runtime windows-phone-8.1 winrt-xaml


    【解决方案1】:

    ListView 和 GridView 支持重新排序项目。考虑使用它们怎么样?

    首先,您可以通过将 CanDragItems CanReorderItems AllowDrop 设置为 false 来禁用此功能,当您将项目保存在 ListView 中时,然后将上述属性设置为 true,这将启用重新排序功能。

    【讨论】:

    • 嗨 JuniperPhoton 坦克的答案。我以前试过这个,这个解决方案对windows phone有问题。在 windows phone 中,你需要将ReorderMode 设置为Enabled,这个动作会导致在holding 事件中没有选择任何项目。并且您需要在按住后松开手指并再次触摸才能移动。喜欢我的解决方案。这是更新触摸输入的任何方式吗?
    • 不过这是真的。使用 ListView 的重新排序功能的体验看起来并不优雅。现在我正在考虑自己写这篇文章。
    • 我需要重新排序系统,例如开始菜单中的重新排序磁贴。你知道它在 windows phone 中是如何工作的吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-16
    • 1970-01-01
    • 2012-01-08
    • 2010-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多