我正在使用列表框拖动控件中的drop元素,以便用户可以对它们进行排序。我能够实现它,但问题是,我不希望用户将元素拖放到列表框之外。目前,如果您这样做,它将从列表框项中删除该元素。
这是我的代码:
<telerik:RadListBox x:Name="SequenceListBox" x:FieldModifier="public"
DockPanel.Dock="Bottom" AllowDrop="True"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
ScrollViewer.VerticalScrollBarVisibility="Hidden"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
Background="Transparent" Margin="30,5,30,5"
ItemTemplate="{DynamicResource ListBoxItemTemplate}">
<telerik:RadListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center"/>
</ItemsPanelTemplate>
</telerik:RadListBox.ItemsPanel>
<telerik:RadListBox.DragVisualProvider>
<telerik:ScreenshotDragVisualProvider />
</telerik:RadListBox.DragVisualProvider>
<telerik:RadListBox.DragDropBehavior>
<telerik:ListBoxDragDropBehavior AllowReorder="True" />
</telerik:RadListBox.DragDropBehavior>
</telerik:RadListBox>
<!-- Sequence ListBox style start -->
<DataTemplate x:Key="ListBoxItemTemplate">
<DataTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="LayoutTransform">
<Setter.Value>
<ScaleTransform ScaleX="1.5" />
</Setter.Value>
</Setter>
</Trigger>
</DataTemplate.Triggers>
<!--<Border BorderThickness="1" BorderBrush="Wheat">-->
<TextBlock Text="{Binding Path=AttributeName}" MinWidth="30" Foreground="Black"
FontSize="12" FontFamily="Segoe UI" ToolTip="Drag and Drop to re-arrange"
VerticalAlignment="Center" HorizontalAlignment="Center" />
<!--</Border>-->
</DataTemplate>
我的问题类似于:this,但我仍然不清楚如何实现它。请帮我解决一下。
此外,我想在用户尝试删除它时突出显示(有一些颜色)2个元素之间的边框,但我不确定如何实现它。
如何修改我的代码,以便列表框在删除控件之外时不会删除项目?
答案 0 :(得分:1)
我自己没有这样做,但你可以看看DragDrop.PreviewDragLeave
。当鼠标将元素拖出控件的边界时,会立即触发此事件。
确定需要取消拖动后,您需要设置一些可以在DragDrop.QueryContinueHandler
中读取的类级变量。遗憾的是,当你在PreviewDragLeave中时,你无法取消拖动,因为eventArgs不支持它。
bool shouldICancelDrag = false
void listBox1_PreviewDragLeave(object sender, DragEventArgs e)
{
shouldICancelDrag = true;
}
private void OnQueryContinueDrag(object sender, QueryContinueDragEventArgs e)
{
if(shouldICancelDrag)
{
e.Action = DragAction.Cancel;
e.Handled = true;
shouldICancelDrag = false;
}
}
编辑: 如果您希望允许它们拖出列表框但不允许它们将项目放在外部(这意味着他们必须将项目返回到列表框或拖动事件将被取消),这里有一个替代方案。
bool shouldICancelDrag = false
//changed this to be previewDrop on the Window (not the listbox)
void window1_PreviewDrop(object sender, DragEventArgs e)
{
//check if the item was dropped on something other than the listbox
//you may need to toy with using e.Source instead (break and see what you're getting via the debugger)
if (! (e.OriginalSource is ListBox))
shouldICancelDrag = true;
}
private void OnQueryContinueDrag(object sender, QueryContinueDragEventArgs e)
{
if(shouldICancelDrag)
{
e.Action = DragAction.Cancel;
e.Handled = true;
shouldICancelDrag = false;
}
}