0

我正在使用列表框在控件中拖放元素,以便用户可以订购它们。我能够实现它,但问题是,我不希望用户将元素拖放到列表框之外。目前,如果你这样做,它会从列表框项目中删除元素。

这是我的代码:

    <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,但我仍然不清楚如何实现它。请帮我解决。

另外,当用户尝试删除它时,我想突出显示(用某种颜色)两个元素之间的边界,但我不确定如何实现这一点。

如何修改我的代码,以便列表框在将项目放在控件之外时不会删除它们?

4

1 回答 1

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;
   }
}
于 2012-11-13T15:10:09.853 回答