【问题标题】:ListBoxDragDropTarget won't reorder elements that use inheritanceListBoxDragDropTarget 不会重新排序使用继承的元素
【发布时间】:2025-12-03 01:15:01
【问题描述】:

我有一个非常奇怪的问题,我不知道如何调试它。 (旁注:我讨厌创建 GUI!无论如何... :)

我一直在关注this 在 Silverlight 上使用拖放操作的指南。 现在,我感兴趣的是列表中的重新排序。

如果我完全按照网站上的示例进行操作,一切都很好。它按预期工作。 但是,当我尝试将元素替换为其他元素时,我无法再重新排序?!

考虑以下 XAML:

<toolkit:ListBoxDragDropTarget AllowDrop="True">
                <ListBox Width="200" Height="500" x:Name="FromBox" DisplayMemberPath="Label">
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel/>
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                </ListBox>
            </toolkit:ListBoxDragDropTarget>

除了此处为“标签”的 DisplayMemberPath 之外,这与我正在关注的文章中的相同。

在代码隐藏中,我现在执行以下操作:

void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            FromBox.ItemsSource = GetElements();
        }

        public static ObservableCollection<Element> GetElements()
        {
            ObservableCollection<Element> Elements = new ObservableCollection<Element>();
            var Label = new Label();
            Label.Label = "Label me";
            Elements.Add(Label);

            var IntegerBox = new IntegerBox();
            IntegerBox.Label = "Størrelsen på en gennemsnits svale:";
            IntegerBox.Description = "cm";
            Elements.Add(IntegerBox);

            var TextBox = new TextBox();
            TextBox.Label = "QTTextBox";
            Elements.Add(TextBox);

            return Elements;
        }

(很抱歉在此位中包含特殊的丹麦字符,但我认为显示我正在使用的确切代码比编辑它更好)

如你所见,我返回了一个 ObservableCollection 元素,每个元素都有一个 Label 属性(显然,它们都继承自 Element 类)。

对我来说,这应该完全与链接中提供的代码相同。 但事实并非如此。好像不能再丢东西了!?

这是它在文章中的样子:

这就是我的元素的外观:

对可以重新排序的东西有一些要求吗?这篇文章使用了 Person-objects,但它们非常简单,并且没有实现任何进一步的接口。如您所见,我还确保使用正确类型的 ObservableCollection。难道就因为它不能处理继承?!

... 还是我只是愚蠢? :-)

提前致谢!

编辑: 我已将问题范围缩小到与继承有关。 以下代码也无法重新排序:

public class People
    {
        public static ObservableCollection<Person> GetListOfPeople()
        {
            ObservableCollection<Person> ppl = new ObservableCollection<Person>();
            for (int i = 0; i < 15; i++)
            {
                if (i % 2 == 0)
                {
                    SomePerson p = new SomePerson() { Firstname = "First " + i.ToString(), Lastname = "Last " + i.ToString() };
                    ppl.Add(p);
                }
                else
                {
                    OtherPerson p = new OtherPerson() {Firstname = "First " + i.ToString(), Lastname = "Last " + i.ToString()};
                    ppl.Add(p);
                }

            }
            return ppl;
        }
    }

    public class Person
    {
        public string Firstname { get; set; }
        public string Lastname { get; set; }
        public string FullName
        {
            get
            {
                return string.Concat(Firstname, " ", Lastname);
            }
        }
    }
    public class SomePerson : Person
    {

    }

    public class OtherPerson : Person
    {

    }

WTF?! 看来您只能重新排序将 specific 类型声明为 ObservableCollection 上的类型参数的元素。因此,如果我声明它包含“Person”对象并混入 Person-objects 而不是 SomePerson 和 OtherPerson,我可以移动 那些

【问题讨论】:

  • 您好 Fafnr,我也遇到了这个问题,我想知道您是否对此有任何解决方案。谢谢你,阿迪
  • 我很抱歉,但我没有... :/ 请参阅下面 SynerCoder 的答案!我已经在 codeplex 上注册以支持那里的问题,看看他们怎么说。

标签: silverlight silverlight-4.0 silverlight-toolkit


【解决方案1】:

我遇到了同样的问题,并进行了更多挖掘。这是我的解决方案:http://cjbhaines.wordpress.com/2011/07/09/silverlight-listboxdragdroptarget-why-cant-i-drop-it-there/

【讨论】:

  • 我自己还没有测试过,但是当我读到它时,听起来不错!今天晚些时候我会测试它,然后你有我的 thx,现在你有我的支持。
  • 非常好!完全有道理!我已将此标记为答案,因为它清楚地回答了我最初的问题,并且提供了非常合理的解决方法。谢谢!
  • 将您的解决方案添加到 silverlight.codeplex 工作项。我希望他们能尽快在工具包的下一个版本中实现它。
  • 谢谢老兄!好像真的救了我。
【解决方案2】:

我有同样的问题并做了几次测试。我的结论是,这不是用户生成的错误,而是工具包中的错误。因此,我提出了一个问题并将其发布给 Silverlight 团队。请对问题投票,以便我们快速得到答复。 http://silverlight.codeplex.com/workitem/8480

如果您认为这有帮助,请标记为答案。

【讨论】:

  • 谢谢。我已经在 codeplex 注册并支持这个问题 - 希望有人回应......
【解决方案3】:

我也想到了一个解决方案,我知道这是一个肮脏的解决方案,但它可以解决问题:

public class People
{
    public static ObservableCollection<PersonWrapper> GetListOfPeople()
    {
        ObservableCollection<PersonWrapper> ppl = new ObservableCollection<PersonWrapper>();
        for (int i = 0; i < 15; i++)
        {
            if (i % 2 == 0)
            {
                SomePerson p = new SomePerson() { Firstname = "First " + i.ToString(), Lastname = "Last " + i.ToString() };
                PersonWrapper pw = new PersonWrapper(){ Person = p };
                ppl.Add(pw);
            }
            else
            {
                OtherPerson p = new OtherPerson() {Firstname = "First " + i.ToString(), Lastname = "Last " + i.ToString()};
                PersonWrapper pw = new PersonWrapper(){ Person = p };
                ppl.Add(p);
            }

        }
        return ppl;
    }
}

public class Person
{
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    public string FullName
    {
        get
        {
            return string.Concat(Firstname, " ", Lastname);
        }
    }
}
public class SomePerson : Person
{

}

public class OtherPerson : Person
{

}

public class PersonWrapper
{
    public Person Person { get; set; }
    public string FullName {
            get{
                    return Person.FullName;
            }
    }
}

我还没有测试过代码,但是现在使用了 1 个类。我知道它很脏,但它仍然不能解决问题。我不知道您的项目可能会有所帮助,但是对于我的项目,此解决方案会带来很多工作。所以希望这个问题能得到快速解决@codeplex

【讨论】:

  • 我喜欢这里可能的解决方法......我在项目之间被抛来抛去,所以我现在无法对其进行测试,但似乎这可以半奏效。不过,和你一样,我把大部分希望寄托在 Codeplex 上