开发者

ListBoxDragDropTarget won't reorder elements that use inheritance

开发者 https://www.devze.com 2023-01-22 20:41 出处:网络
I have a very strange issue and I have no idea how to debug it. (Side note: I hate creating GUI\'s! Anyway... :)

I have a very strange issue and I have no idea how to debug it. (Side note: I hate creating GUI's! Anyway... :)

I have been following this guide to using drag 'n drop on Silverlight. Right now, what I'm interested in is the reordering in a list.

If I follow the example from the site exactly, everything is fine. It works as expected. But, when I try to substitute the elements for something else, I can no longer reorder?!

Consider the following 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>

aside from the DisplayMemberPath which is here "Label", this is the same as in the article I'm following.

In the co开发者_如何学Cde-behind I now do the following:

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

(Apologies for containing special Danish characters in this bit, but I thought it was better to show the exact code I'm using than editing it out)

As you can see, I return an ObservableCollection of Element's, each of which has a Label property (and they all inherit from the Element class, obviously).

To me, this ought to work exactly the same as the code provided in the link. But it doesn't. It's as if I can no longer drop items!?

This is how it looks in the article:

ListBoxDragDropTarget won't reorder elements that use inheritance

And this is how it looks with my elements:

ListBoxDragDropTarget won't reorder elements that use inheritance

Is there some demand on the kind of things that can be reordered? The article uses Person-objects, but these are ultra simple and do not implement any further interfaces. I'm also, as you can see, making sure to use an ObservableCollection with the right type. Is it just because it can't handle inheritance?!

... Or have I simply been stupid? :-)

Thanks in advance!

EDIT: I've narrowed down the problem to having to do with inheritance. The following code cannot be reordered either:

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?! It seems you can only reorder elementes that have the specific type declared as the type-parameter on the ObservableCollection. Thus, if I declare it contains "Person" objects and mix in Person-objects rather than SomePerson and OtherPerson, I can move those.


I had the same problem and did a bit more digging. Heres my solution: http://cjbhaines.wordpress.com/2011/07/09/silverlight-listboxdragdroptarget-why-cant-i-drop-it-there/


I have the same problem and did several tests. My conclusion is that this isn't a user generated error but rather a bug in the toolkit. Therefor I have made an issue out of the matter and posted it to the silverlight team. Please vote the issue up so we could get a quick answer. http://silverlight.codeplex.com/workitem/8480

Please mark as answer if you think this could help.


I also thought of a solution,, I know it is a dirty one but it does the trick:

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

I haven't tested the code yet but Now there is 1 Class used. I know it is dirty,, and it still doesnt solve the problem. I don't know about your project maybe this helps, but for my project this solution would bring to much work with it. So hoping the issue will get a quick fix @ codeplex

0

精彩评论

暂无评论...
验证码 换一张
取 消