1

嗨,我尝试在ItemsControl中重新订购items。当我设置ItemsControl元素ManipulationModeManipulationModes.TranslateYScrollView dosent工作。对于这个问题,我在Holding事件中更改了ManipulationMode,但我遇到了一个新问题。 在我的代码中,我们需要释放手指后,再次触摸移动。我需要握住并移动手指拖动没有握住和释放的物品,然后再次触摸并移动。此版本对我和用户 不利,请帮帮我。如何通过持有物品来重新排序itemcontrol中的物品?

我的代码:

<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(); 
     // } 
    } 

}

回答

1

ListView控件& GridView控件支持重新排序项。思考如何使用它们?

首先,你可以通过设置CanDragItemsCanReorderItemsAllowDrop假,当你在你的ListView按住该项目,然后上面设置为true的属性,这将使重新排序功能,禁用此功能。

+0

您好JuniperPhoton坦克为答案。我之前尝试过,这个解决方案对于windows phone有问题。在Windows Phone中,您需要将'ReorderMode'设置为'Enabled',并且此操作会导致不会选择'holding'事件中的任何项目。你需要松开你的手指,然后再次触摸移动。像我的解决方案。任何方式来更新tuch输入? –

+0

虽然这是真的。使用ListView的重新排序功能的体验看起来不那么优雅。现在我正在考虑自己写这个。 – JuniperPhoton

+0

我需要在开始menu.do重新排序瓷砖重新排序系统你知道如何在Windows Phone中工作吗? –