ListBoxDragDropTarget :可以让它工作

发布于 2024-11-02 18:37:21 字数 2976 浏览 4 评论 0原文

我有一个 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>

I have a Silverlight 4 Business Application, I've installed Silverlight Toolkit April 2011 and added the reference to the project. I want to to reorder a listbox using drag and drop.

My code is the following:

<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>

And

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

When trying to drag an item I see the "ghost" of the item and an icon whth two arrows (an up and a down arrow) but when I drop it (in the same listbox) nothing happens! It doesnt get reordered.

What am I doing wrong???
Do I have to listen to an event and implement something??

Thanks!

EDIT: The complete code is:

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

壹場煙雨 2024-11-09 18:37:22

你可以试试这个吗?

        <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;

can you pls try this?

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

Sorry I was wrong.

If you do this it will work.

        var list = new ObservableCollection<int>();

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

        listBox1.ItemsSource = list;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文