ok... this is the original code I had after updating it to SL2 List<UIElement> elements = VisualTreeHelper .FindElementsInHostCoordinates(e.GetPosition(null), this).ToList();
for (int i = 0; i < elements.Count; i++) { ListBox lb = elements[i] as ListBox; if (lb != null) { .... } } then I changed it to this ListBox lb = (from el in VisualTreeHelper .FindElementsInHostCoordinates(e.GetPosition(null), this) where el.GetType().Name == "ListBox" select el).SingleOrDefault() as ListBox; if (lb != null) { .... } and now I change the first line to this... ListBox lb = (from el in VisualTreeHelper .FindElementsInHostCoordinates(e.GetPosition(null), this) select el).Where(el => el.GetType().Name == "ListBox").SingleOrDefault() as ListBox; if (lb != null) { .... } so which is the best way and why do you think it? .Where(el => el.GetType().Name == "ListBox") is this better/going to cost more than the "for" loop...? On Wed, Oct 22, 2008 at 12:27 PM, Jordan Knight <[EMAIL PROTECTED]>wrote: > +1 > > As Jeffrey Richter says: > > Always take the least specific and return the most specific :) > > -----Original Message----- > From: [EMAIL PROTECTED] [mailto: > [EMAIL PROTECTED] On Behalf Of ross jempson > Sent: Wednesday, 22 October 2008 1:20 PM > To: listserver@ozsilverlight.com > Subject: Re: [OzSilverlight] Drop & Drag - DataGridRow > > this isn't really a c# list, but i will respond. > > ok, to be pedantic i think the only reason it works is because you are > lucky the method you are calling is storing the the data internally as > List<UIElement>, or something that can be cast to List<UIElement>. > > but you are programming against an interface, IEnumerable<UIElement>, > and your code will break if the implementation of the method you are > calling changes. And you may not have control over that. > > consider the console app below. the first cast works, iterating over > the IEnumerable works in the second case, but the third causes a > runtime error because the code has assumed it knows how the > GetStringsFromQueue() method is storing the data internally. > > class Program > { > static void Main(string[] args) > { > // Works by luck > ((List<string>)GetStringsFromList()).ForEach(i => > Console.WriteLine(i)); > Console.ReadKey(); > > > // Works as designed > foreach (string s in GetStringsFromQueue()) > { > Console.WriteLine(s); > } > Console.ReadKey(); > > // Runtime error > ((List<string>)GetStringsFromQueue()).ForEach(i => > Console.WriteLine(i)); > Console.ReadKey(); > > > } > > private static IEnumerable<string> GetStringsFromQueue() > { > System.Collections.Generic.Queue<string> ret = new > Queue<string>(); > > ret.Enqueue("D"); > ret.Enqueue("E"); > ret.Enqueue("F"); > > return ret; > > } > > private static IEnumerable<string> GetStringsFromList() > { > List<string> ret = new List<string>(); > > ret.Add("A"); > ret.Add("B"); > ret.Add("C"); > > return ret; > } > } > > > > > > On Wed, Oct 22, 2008 at 10:52 AM, .net noobie <[EMAIL PROTECTED]> > wrote: > > no it works > > > > On Wed, Oct 22, 2008 at 10:15 AM, ross jempson <[EMAIL PROTECTED]> > > wrote: > >> > >> Wouldn't this option you presented create a runtime cast error anyway? > >> > >> List<UIElement> elements = > >> > >> > (List<UIElement>)VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(null),this); > >> > >> Hence .ToList() is better I would suggest. > >> > >> Another thing to consider is whether you actually need a List<> which > >> forces the list to be fully populated. Leaving it as an IEnumerable<> > >> will be more effecient in some scenarios, as the datasource may be > >> lazy loading. > >> > >> > >> On Wed, Oct 22, 2008 at 2:26 AM, .net noobie <[EMAIL PROTECTED]> > >> wrote: > >> > Thanks, I found this link for a ListBox version > >> > needed to update the .HitTest to SL2 and it works fine > >> > I only wanted a single column, I used a DataGrid because I was being > >> > lazy > >> > and wanted to use the DataGrid Header, but I will just make that for a > >> > ListBox :) > >> > changed this line for SL2 > >> > List<UIElement> elements = > >> > (List<UIElement>)this.HitTest(e.GetPosition(null)); > >> > to > >> > List<UIElement> elements = > >> > > >> > > (List<UIElement>)VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(null), > >> > this); > >> > > >> > > >> > > http://cid-71b364b59919d1e8.skydrive.live.com/self.aspx/Public/blog%20files/dragdrop|_updated.zip > >> > which is the better way to do this line? > >> > List<UIElement> elements = > >> > > >> > > (List<UIElement>)VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(null), > >> > this); > >> > or > >> > List<UIElement> elements = > >> > VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(null), > >> > this).ToList(); > >> > which one takes more work under the covers to make the conversion to a > >> > list? > >> > > >> > On Tue, Oct 21, 2008 at 8:57 PM, Valentin Stoychev > >> > <[EMAIL PROTECTED]> wrote: > >> >> > >> >> I hope you will not treat this as an advertisement. > >> >> > >> >> Take a look at this example from the Telerik QuickStart Application. > >> >> http://demos.telerik.com/silverlight/#Examples/DragAndDrop/FirstLook > >> >> > >> >> It is using the Telerik Drag/Drop framework that do exactly what you > >> >> need, > >> >> but with listboxes. Modifying the sample a little bit will do the job > >> >> for a > >> >> DataGrid. > >> >> > >> >> Regards, > >> >> Valentin Stoychev > >> >> Telerik > >> >> > >> >> On Tue, Oct 21, 2008 at 1:05 PM, Stephen Price > >> >> <[EMAIL PROTECTED]> > >> >> wrote: > >> >>> > >> >>> I got drag and drop between listboxes working in a prototype I was > >> >>> working on few months ago. > >> >>> I used canvas's and a method that checked for a collision/overlap > >> >>> between > >> >>> the two elements when you did the drop. > >> >>> Used the standard mouse approach of move events while button down to > >> >>> animate the dragging around part. Had to clone the image element > >> >>> myself as > >> >>> no clone in Silverlight. I've since had an idea of another way to > copy > >> >>> the > >> >>> image but haven't been back to test out my idea. (maybe using brush > >> >>> source - > >> >>> you know how you can have video brushes all pointing at same source? > >> >>> wondering if that would work with an image brush). > >> >>> hope that makes sense... > >> >>> cheers, > >> >>> Stephen > >> >>> > >> >>> On Tue, Oct 21, 2008 at 4:00 PM, .net noobie < > [EMAIL PROTECTED]> > >> >>> wrote: > >> >>>> > >> >>>> I would like to be able to Drop & Drag a DataGridRow from one > >> >>>> DataGrid > >> >>>> to another...? > >> >>>> does anyone have any tips or links on how I would do this please? > >> >>>> > >> >>>> -- > >> >>>> .net noobie(tm) > >> >>>> > >> >>>> ------------------------------------------------------------------- > >> >>>> OzSilverlight.com - to unsubscribe from this list, send a message > >> >>>> back to > >> >>>> the list with 'unsubscribe' as the subject. > >> >>>> Powered by mailenable.com - List managed by www.readify.net > >> >>> > >> >>> ------------------------------------------------------------------- > >> >>> OzSilverlight.com - to unsubscribe from this list, send a message > back > >> >>> to > >> >>> the list with 'unsubscribe' as the subject. > >> >>> Powered by mailenable.com - List managed by www.readify.net > >> >> > >> >> ------------------------------------------------------------------- > >> >> OzSilverlight.com - to unsubscribe from this list, send a message > back > >> >> to > >> >> the list with 'unsubscribe' as the subject. > >> >> Powered by mailenable.com - List managed by www.readify.net > >> > > >> > > >> > -- > >> > .net noobie(tm) > >> > > >> > ------------------------------------------------------------------- > >> > OzSilverlight.com - to unsubscribe from this list, send a message back > >> > to > >> > the list with 'unsubscribe' as the subject. > >> > Powered by mailenable.com - List managed by www.readify.net > >> > >> > >> ------------------------------------------------------------------- > >> OzSilverlight.com - to unsubscribe from this list, send a message back > to > >> the list with 'unsubscribe' as the subject. > >> Powered by mailenable.com - List managed by www.readify.net > >> > >> > > > > > > > > -- > > .net noobie(tm) > > > > ------------------------------------------------------------------- > > OzSilverlight.com - to unsubscribe from this list, send a message back to > > the list with 'unsubscribe' as the subject. > > Powered by mailenable.com - List managed by www.readify.net > > > ------------------------------------------------------------------- > OzSilverlight.com - to unsubscribe from this list, send a message back to > the list with 'unsubscribe' as the subject. > Powered by mailenable.com - List managed by www.readify.net > > > > > ------------------------------------------------------------------- > OzSilverlight.com - to unsubscribe from this list, send a message back to > the list with 'unsubscribe' as the subject. > Powered by mailenable.com - List managed by www.readify.net > > > -- .net noobieâ„¢ ------------------------------------------------------------------- OzSilverlight.com - to unsubscribe from this list, send a message back to the list with 'unsubscribe' as the subject. Powered by mailenable.com - List managed by www.readify.net