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 code-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:
And this is how it looks with my elements:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With