- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试拖放我的 Gong Solutions 以对单个列表进行排序。当我拖放一个项目时,它会从列表中删除该项目,我可以看到它从界面上消失了。看来 DragOver
方法没有先移动对象,所以当 Drop
方法触发时,它只是删除了项目。
当我删除属性 dd:DragDrop.DropHandler="{Binding}"
时,界面上的拖放操作正常。但是,我必须触发事件,这样我才能知道列表何时被重新排序。
XAML:
<Window x:Class="Reorder_item_WPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:dd="clr-namespace:GongSolutions.Wpf.DragDrop;assembly=GongSolutions.Wpf.DragDrop">
<Grid>
<ListBox Grid.Column="1" SelectionMode="Extended" ItemsSource="{Binding MSPCollection}"
dd:DragDrop.IsDragSource="True" Width="300" Margin="0,0,5,0"
dd:DragDrop.IsDropTarget="True"
dd:DragDrop.DropHandler="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Background="#2ba3d5" Height="50" Width="280">
<TextBlock Drop="TextBlock_Drop" Text="{Binding Name}"
Foreground="White"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="40"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
C#:
public class MSP {
public int Id { get; set; }
public string Name { get; set; }
}
class MainViewModel : IDropTarget
{
public ObservableCollection<MSP> MSPCollection { get; set; }
public MainViewModel() {
MSPCollection = new ObservableCollection<MSP>();
MSPCollection.Add(new MSP() {
Id = 1,
Name = "Anis Derbel"
});
MSPCollection.Add(new MSP()
{
Id = 2,
Name = "Firas Mdimagh"
});
MSPCollection.Add(new MSP()
{
Id = 3,
Name = "Khaled Jemni"
});
MSPCollection.Add(new MSP()
{
Id = 4,
Name = "Sahbouch"
});
}
public void DragOver(IDropInfo dropInfo) {
if (dropInfo.Data is MSP) {
dropInfo.DropTargetAdorner = DropTargetAdorners.Highlight;
dropInfo.Effects = DragDropEffects.Move;
}
}
public void Drop(IDropInfo dropInfo) {
MSP msp = (MSP)dropInfo.Data;
((IList)dropInfo.DragInfo.SourceCollection).Remove(msp);
}
}
最佳答案
DragOver
不会删除任何项目。您正在使用 Drop
方法删除项目。您不应该在 DragOver
方法中进行任何删除或添加,这就是 Drop
方法的用途。 DragOver
应该只用于设置目标装饰器和效果。我使用 ILSpy 查看他们的代码(见下文),这有助于阐明这里发生的事情。他们有一个名为 DefaultDropHandler : IDropTarget
的类,顾名思义,如果未使用附加属性 dd:DragDrop.DropHandler
分配,则该类是默认的放置处理程序。因此,当您删除 dd:DragDrop.DropHandler="{Binding}"
行时,这就是用于放置操作的内容。查看他们的代码,一切都为您处理,即添加和删除项目,并在此过程中进行大量错误检查和索引控制。
当您确实将放置处理程序设置为您的 ViewModel 时,默认处理程序中的任何代码都不会执行,因为您将该处理程序替换为您的 ViewModel。因此,您必须完全处理跌落。换句话说,你必须做所有的错误和类型检查,删除项目和添加项目(如果你有多个列表),并保持正确的顺序。在执行任何删除和添加之前,您可能还想检查源集合是否与目标集合不同,但这仍然不会将它们添加到正确的位置。
因为您只有一个列表,所以不要在 Drop
方法中删除该项目。但是,由于我上面提到的原因,什么都不会发生。如果您确实有多个列表,可以按照以下方法将项目从一个列表移动到另一个列表:
public void Drop(IDropInfo dropInfo)
{
MSP msp = (MSP)dropInfo.Data;
if(dropInfo.DragInfo.SourceCollection != dropInfo.TargetCollection)
{
((IList)dropInfo.DragInfo.SourceCollection).Remove(msp);
((IList)dropInfo.TargetCollection).Add(msp);
}
}
如果您需要的话,您必须按照正确的顺序排列元素。作为完成所有这些工作的替代方法,您可以利用和扩展它们的默认处理程序。他们制作了 Drop
方法 virtual
,因此您可以让您的 View 模型继承自 DefaultDropHandler
(即 class MainViewModel : DefaultDropHandler
)而不是实现 IDropTarget
接口(interface)。然后,简单地覆盖 Drop
方法,并调用基本方法。像这样:
public override void Drop(IDropInfo dropInfo)
{
base.Drop(dropInfo);
//do other stuff
}
如果需要,您也可以用相同的方式重写 DragOver
方法,但如果不需要,它将只使用默认行为。
如果你很好奇,下面是 Gong 在你没有分配时使用的默认处理程序:
public virtual void Drop(IDropInfo dropInfo)
{
if (dropInfo != null && dropInfo.DragInfo != null)
{
int insertIndex = (dropInfo.InsertIndex != dropInfo.UnfilteredInsertIndex) ? dropInfo.UnfilteredInsertIndex : dropInfo.InsertIndex;
ItemsControl itemsControl = dropInfo.VisualTarget as ItemsControl;
if (itemsControl != null)
{
IEditableCollectionView editableItems = itemsControl.Items;
if (editableItems != null)
{
NewItemPlaceholderPosition newItemPlaceholderPosition = editableItems.NewItemPlaceholderPosition;
if (newItemPlaceholderPosition == NewItemPlaceholderPosition.AtBeginning && insertIndex == 0)
{
insertIndex++;
}
else if (newItemPlaceholderPosition == NewItemPlaceholderPosition.AtEnd && insertIndex == itemsControl.Items.Count)
{
insertIndex--;
}
}
}
IList destinationList = dropInfo.TargetCollection.TryGetList();
List<object> data = ExtractData(dropInfo.Data).OfType<object>().ToList();
List<object>.Enumerator enumerator;
if (!ShouldCopyData(dropInfo))
{
IList sourceList = dropInfo.DragInfo.SourceCollection.TryGetList();
if (sourceList != null)
{
enumerator = data.GetEnumerator();
try
{
while (enumerator.MoveNext())
{
object o2 = enumerator.Current;
int index = sourceList.IndexOf(o2);
if (index != -1)
{
sourceList.RemoveAt(index);
if (destinationList != null && object.Equals(sourceList, destinationList) && index < insertIndex)
{
insertIndex--;
}
}
}
}
finally
{
((IDisposable)enumerator).Dispose();
}
}
}
if (destinationList != null)
{
TabControl tabControl = dropInfo.VisualTarget as TabControl;
bool cloneData = dropInfo.Effects.HasFlag(DragDropEffects.Copy) || dropInfo.Effects.HasFlag(DragDropEffects.Link);
enumerator = data.GetEnumerator();
try
{
while (enumerator.MoveNext())
{
object o = enumerator.Current;
object obj2Insert = o;
if (cloneData)
{
ICloneable cloneable = o as ICloneable;
if (cloneable != null)
{
obj2Insert = cloneable.Clone();
}
}
destinationList.Insert(insertIndex++, obj2Insert);
if (tabControl != null)
{
TabItem obj = tabControl.ItemContainerGenerator.ContainerFromItem(obj2Insert) as TabItem;
if (obj != null)
{
obj.ApplyTemplate();
}
tabControl.SetSelectedItem(obj2Insert);
}
}
}
finally
{
((IDisposable)enumerator).Dispose();
}
}
}
}
关于c# - Gong Solutions 拖放排序列表从列表中删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53877125/
我在这里有一个我想要做的事情的例子:http://jsbin.com/OwoYAlEQ/1/edit 这是我的 HTML: person one person two person three per
我想知道是否有人知道是否有一个预先制定的解决方案:我在 ASP.net 网站上有一个列表,我希望用户能够通过拖放对列表进行重新排序。此外,我希望有第二个列表,用户可以将第一个列表中的项目拖到其中。 到
我想知道是否有人知道是否有一个预先制定的解决方案:我在 ASP.net 网站上有一个列表,我希望用户能够通过拖放对列表进行重新排序。此外,我希望有第二个列表,用户可以将第一个列表中的项目拖到其中。 到
我在我的 Web 应用程序中使用 Ajax ControlToolkit 中的 ModalPopupExtender。我将其 Drag 属性设置为 true,但是当我拖动弹出面板并将其放到新位置时,它
所以,基于this answer ,我有一组可以拖放并卡入到位的 div。唯一的问题是,可拖动的 div 具有不同的高度,我需要它们始终捕捉到目标的底部,而不是顶部。 您可以在this JsFiddl
我一直在使用 Bea 的解决方案 here一段时间后发现它非常有帮助。现在我遇到的问题是,当我将项目拖放到另一个 ListView 控件中或拖放到另一个 ListView 控件中,并且我想在拖动“期间
我试图在使用 QTreeWidget.setItemWidget() 重新父级(拖放)后将小部件放入 QTreeWidgetItem 但是,如果编译以下代码,结果是 QTreeWidgetItem 内
这是场景,我使用的是prototype和scriptaculous,但我相信jquery也会有同样的问题。我在相对定位的 div 中有一个可拖动图像的列表。问题是我无法将图像拖出父 div...好吧.
我正在使用一个普通(Bootstrap)表,我想在其中包含可排序的行。我正在使用 Angular CDK (DragDropModule) 来实现排序/排序。但是,当拖动行时,它会扭曲宽度,因为 cd
我正在尝试在我的 UICollectionView 中实现拖放机制,这与在快捷方式应用程序中重新排序快捷方式的组件非常相似。 截至目前,行为是当您开始拖动时,会留下一个透明的单元格 View ,而另一
我有以下 Jquery UI 拖放 jsfiddle https://jsfiddle.net/zoojsfiddle/ud96jdcp/ 具有
我希望创建一个基于网络的“公告板”,可以这么说,用户可以在其中创建/删除/拖放“图钉”,而不允许重叠“图钉”。 这是一个图表,应该有助于说明我正在尝试创建的内容: 'pins' 可能已创建双击;他们会
我是一名优秀的程序员,十分优秀!