ListBox拖放目标:可以让它工作

时间:2011-04-21 04:14:47

标签: silverlight-4.0 silverlight-toolkit

我有一个Silverlight 4业务应用程序,我在2011年4月安装了Silverlight Toolkit并添加了对该项目的引用。我想使用拖放重新排序列表框。

我的代码如下:

<Grid x:Name="LayoutRoot">
    <toolkit:ListBoxDragDropTarget AllowDrop="True">
        <ListBox  AllowDrop="True" Name="listBox1">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Vertical"></StackPanel>

                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
        </ListBox>
    </toolkit:ListBoxDragDropTarget>
  </Grid>

private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            listBox1.ItemsSource = new Int32[] {1,2,3,4,5 };
        }

当试图拖动项目时,我看到项目的“幽灵”和带有两个箭头(向上和向下箭头)的图标但是当我放下它时(在同一个列表框中)没有任何反应!它没有重新排序。

我做错了什么??? 我是否必须听一个事件并实施一些事情?

谢谢!

编辑:完整的代码是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;

namespace editorencuestas
{
    public partial class testdragdrop : Page
    {
        public testdragdrop()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            listBox1.ItemsSource = new Int32[] {1,2,3,4,5 };
        }

    }
}

AND

<navigation:Page x:Class="editorencuestas.testdragdrop" 
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
           mc:Ignorable="d"
           xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
           d:DesignWidth="640" d:DesignHeight="480"
           Title="testdragdrop Page" xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" Loaded="Page_Loaded">
    <Grid x:Name="LayoutRoot">
        <toolkit:ListBoxDragDropTarget AllowDrop="True">
            <ListBox x:Name="listBox1" Height="175" Width="147">
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel />
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
            </ListBox>
        </toolkit:ListBoxDragDropTarget>
        </Grid>
</navigation:Page>

1 个答案:

答案 0 :(得分:0)

你能试试吗? <击>

<击>
        <ListBox x:Name="yourListBox">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel/>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
        </ListBox>

<击>

抱歉,我错了。

如果你这样做会有效。

        var list = new ObservableCollection<int>();

        list.Add(1);
        list.Add(2);
        list.Add(3);

        listBox1.ItemsSource = list;
相关问题