【发布时间】: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