Xaml 如何在itemscontrol中通过保留项来重新排序项?

Xaml 如何在itemscontrol中通过保留项来重新排序项?,xaml,drag-and-drop,windows-runtime,windows-phone-8.1,winrt-xaml,Xaml,Drag And Drop,Windows Runtime,Windows Phone 8.1,Winrt Xaml,您好,我尝试在ItemsControl中对项目重新排序。当我将ItemsControl中元素的operationmode设置为operationmodes.TranslateYScrollViewdosent work时。对于这个问题,我在保持事件中更改了操作模式,但我遇到了一个新问题。 在我的代码中,我们需要在握住手指后松开手指,然后再次触摸才能移动。我需要握住并移动手指来拖动未握住并松开的项目,然后再次触摸并移动。这个版本对我和用户都不好 请帮帮我 我的代码: <ItemsContro

您好,我尝试在
ItemsControl
中对
项目重新排序。当我将
ItemsControl
中元素的
operationmode
设置为
operationmodes.TranslateY
ScrollView
dosent work时。对于这个问题,我在
保持
事件中更改了
操作模式
,但我遇到了一个新问题。 在我的代码中,我们需要在握住手指后松开手指,然后再次触摸才能移动。我需要握住并移动手指来拖动未握住并松开的项目,然后再次触摸并移动。这个版本对我和用户都不好 请帮帮我

我的代码:

<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();
       // }
    }
公共类DragReOrderBehavior:DependencyObject,IBehavior
{
私有静态只读int AutoScrollHitRegionSize=80;
私人调度员(autoScrollTimer);;
私有框架元素_dragItem;
私人int_初始绘图索引;
private bool IsActive=false;
私有项控制项控制列表;
私有项目视图模型draggedItemVM;
私有主视图模型VM;
私有ResettableObservableCollection itemsListVm;
私有ScrollViewer\u ScrollViewer;
//私人音响效果(moveSound);;
public DependencyObject AssociatedObject{get;private set;}
公共无效附加(DependencyObject associatedObject)
{
AssociatedObject=AssociatedObject;
var fw=(FrameworkElement)AssociatedObject;
//fw.manufactionmode=manufactionmodes.TranslateY | manufactionmodes.System;
////fw.操纵开始+=fw_操纵开始;
fw.Holding+=fw_Holding;
//fw.manipionDelta+=fw_manipionDelta;
//fw.ManipulationCompleted+=fw_ManipulationCompleted;
if(fw.RenderTransform==null | | fw.RenderTransform as TranslateTransform==null)
{
fw.RenderTransform=新的TranslateTransform();
}
//一种定时器,用于周期性地检测传感器的位置
//拖动项目以允许自动滚动行为
_autoScrollTimer=新调度程序();
_autoScrollTimer.Interval=TimeSpan.From毫秒(50);
_autoScrollTimer.Tick+=(s,e)=>
{
AutoScrollList();
ShuffleItemsOnDrag();
};
//_moveSound=SoundEffect.FromStream(TitleContainer.OpenStream(“Sounds/windowsxp Menu Command.wav”);
}
无效FWU保持(对象发送器,保持路由目标e)
{
//if(Math.Abs(e.Cumulative.Translation.Y)>Math.Abs(e.Cumulative.Translation.X))
// {
//var fw=(FrameworkElement)AssociatedObject;
//fw.manufactionmode=manufactionmodes.TranslateRailsY | manufactionmodes.System;
IsActive=true;
//找到要拖动的元素
_dragItem=关联对象作为框架元素;
itemscontrolList=AssociatedObject.Orients().OfType().FirstOrDefault();
draggedItemVM=((ItemViewModel)\u dragItem.DataContext);
VM=(MainViewModel)itemscontrolList.DataContext;
itemsListVm=VM.Items;
_scrollViewer=itemscontrolList.subjects()
.Cast()
.SingleOrDefault();
_dragItem.SetVerticalOffset(0);
_dragItem.manufactionMode=manufactionModes.TranslateY;
_dragItem.ManipulationDelta+=fw_ManipulationDelta;
_dragItem.ManipulationCompleted+=fw_ManipulationCompleted;
_dragItem.UpdateLayout();
itemscontrolList.UpdateLayout();
_initialDragIndex=itemsListVm.IndexOf(draggedItemVM);
//淡出列表中的项目,而不是拖动的项目
foreach(itemscontrolList.GetItemsInView()中的变量项)
.Where(i=>i.DataContext!=draggedItemVM))
{
项目.动画(1.0,0.7,“不透明度”,300,0);
}
_autoScrollTimer.Start();
// }
}

}

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


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

嗨,我想知道答案。我以前试过这个,这个解决方案对windows phone有问题。在windows phone中,需要将
ReorderMode
设置为
Enabled
,此操作会导致在
holding
事件中不选择任何项目。握住后需要松开手指,再次触摸才能移动。我喜欢我的解决方案。这是更新tuch输入的方法吗?是的。使用ListView的重新排序功能的体验看起来并不优雅。现在我正在考虑自己写这篇文章。我需要像“开始”菜单中的“重新排序磁砖”这样的重新排序系统。你知道它在windows phone中是如何工作的吗?