Simple trick for getting right the DataGrid’s SelectedItem

I have been developing a Silverlight business application and one of the problems I have found is the “inconsistency” of the DataGrid… at least of some of its behaviors, when I click on a row, I expect that the SelectedItem (the clicked one) is set on the corresponding DataGrid property. 

But it does not. at least not “always”, lets be clear maybe I’m doing something wrong but this randomness on this behavior is driving me crazy, if I click on a Grid row, it should select it and mark the SelectedItem on the corresponding property, right? 

I have been doing so in the MouseLeftButtonUp of the DataGrid, so that’s what I expected but… sometimes it had it, sometimes not… bummer! 

So I have been trying to detect why is this happening but could not discover it… maybe I’m wrong in any of the assumptins but meanwhile, I got a workaround that does really work :). 

The following code explains it all… 

void datagrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { 

  if (datagrid.SelectedItem == null){ 

 

      FrameworkElement fe = (e.OriginalSource as FrameworkElement); 

 

      FrameworkElement feCell = (fe.Parent as FrameworkElement); 

 

      var myData = feCell.DataContext; 

  } 

} 

Basically I check first If I have the SelectedItem set, sometimes it is, so then I avoid doing the following trick.

The “trick” mainly gets the originalsource that is launching the event, that is the control inside the cell of the grid,  most of times it will be a TextBox. Second I get the Parent, with this I am getting to the Cell.

Note that if you have a complex structure you should set up a recursive function to go “up” until you get a reference for the cell.

From the cell you have the datacontext which is the item whe should have got from the SelectedItem property.  you should cast it to the inherent type and get any value you need from it.

Also, please note that the DataContext could be caught from the first element (OriginalSource) but it’s more ellegant to get it from the Cell, and if it is a complex custom cell this also asures that it gets the DataContext properly.

And yes, it works 100% 😉

Happy coding!

PS: If somebody knows what I have done wrong or why the DataGrid.SelectedItem Property does not return the clicked item, I would be happy to hear. 🙂

Trick for solving WCF RIA Services issue..

I am having the following issue: Sometimes, when I am issuing a DomainService call using WCF RIA Services, I got the callback right but the loadoperation I have assigned has no results, meaning no entities are returned and the “IsComplete” property is false. 

The following code resumes what I am doing, including the LoadOperation definition, the calling function and the callback function: 

LoadOperation lo; 

private void CallDomainService(){ 

DomainContext dc =  new DomainContext(); 

lo = dc.Load(dc.GetDataQuery()); 

lo.Completed += new EventHandler(lo_Completed); 

} 

And it executes well and calls properly lo_completed… 

void lo_Completed(object sender, EventArgs e){ 

     if ((lo != null) && (lo.IsComplete == true)){

      if ((lo.Entities !=null) && (lo.Entities.Count() >= 1))         //Get the returned entity or whatever..

   }

} 

The issue is that I am getting properly the details of the load Operation, but some random times, it does not return the object properly, it is not null but its .IsComplete property is False and the Entities collection is empty, though that it has had been processed properly by the server.

The trick or workaround here is to get, in this exact case, the loadoperation from the server, which is a reference to the right load operation. The code should be like this:

LoadOperation<EntityType> lo2 = (LoadOperation<EntityType>)sender;

if ((lo2.IsComplete) && (lo2.Entities.Count() >= 1)) {

   EntityType Entity = lo2.Entities.First();

 

}

And with this, problem solved.

Are you having this random issue or others? anybody found why this is happening?

Windows phone performance trick

The WP7 emulator can be painfully slow if you don’t check two simple things,

1. One, is check the DDI version and the driver model, for doing this you should go to Windows button  and on the search/execute area type dxdiag & pulse return. There should appear the DirectX diagnostic tool, once there click one of the Display tabs and on the controllers, validate the DDI version. It should be 10 or better. Also check the Dirver model, should be WDDM 1.1 or better – if it appears, as it does on one of my laptops but on my desktop it does not – note that I have a three monitor display working with two GPU, so I guess that’s why…

2. Second, enable the BIOS virtualization support, as this is set to disabled on most motherboards. This can significantly increase the emulator speed as it will run directly on the CPU which is better than a layer of software – as a “man in the middle” which is logically slower.. this will mean getting into your BIOS and rebooting your system.

For more insight on WP7 performance, this talk is a must : http://live.visitmix.com/MIX10/Sessions/CL60

Have fun!

3D (2.5D) Interaction Experimental site

If you have followed me on my latest posts you should have seen some of my Behavior experiments for 3D interaction, trying to emulate already existing Real world & 2D interaction design patterns as MouseOver, Click, etc… in 3D (or Silverlight’s actual 2.5D, for Desktop & Windows Phone).

I have put the site www.bcndev.net as a sample or playground thinking on nothing but on providing a 3D interactive experience, with the points that I am not being paid for (it’s a volunteer work for promoting the Barcelona Developers User’s group event) and I have thought on nothing (I repeat myself but I want this clear) apart from the experience, so do not expect a great degree of indexability, accessibility, usability or even performance… the Matrix background effect is a killer of CPU resources …

Disclaimers: If I had the time, hopefully I will do it “when I can” I will implement a “effect adjuster” for adapting it to reduce depth layers & elements on each layer and, maybe, even the falling speed… to adapt it to get a decent framerate… I’ve got on mind other optimizations but sadly not the time, so sorry in advance if this is going too slow for you to appreciate the 3D interactions.
I have added GPU acceleration & left all the debugging on for you to be able to see the performance. It should not go very bad if you have a decent GPU so please leave me a message if it does not, and include the FPS. Thanks!!!

This site is using the 3D behaviors I have published at the Expression Gallery and some others that I have yet to polish before publishing, some of the new ones are part of an experiment of providing 3D effects to 2D elements (usercontrols in fact) and allowing them to nest their DataContexts and thus providing an interesting way of navigating the information. This has yet to be polished but you can see it working when you press the “+ Info” button and then the “Ver Detalles” button…” then just also press over a session or speaker…
As for the behaviors, you can see & get them here:
http://gallery.expression.microsoft.com/en-us/Project3DToMouse
http://gallery.expression.microsoft.com/en-us/Slide3DToMouse
http://gallery.expression.microsoft.com/en-us/MouseOver3D
http://gallery.expression.microsoft.com/en-us/Loop3d
http://gallery.expression.microsoft.com/en-us/Animate3D

I would greatly appreciate any feedback you can provide, thanks!!

And, as always, have fun!

Note: The matrix effect is an evolved version of the original code from Terence Tsang, http://www.shinedraw.com – thanks Terence for all those great samples :).

Animate3D, another Silverlight behavior

Animate3D is a a Silverlight Design Interaction Pattern for making easier to implement 3D Interaction – In this case the effect provided is 3D animation that launches in response to a concrete event providing a PlaneTransform animation which can animate a target object with the three Axis, X, Y, Z and its Global and Local offsets. It also allows us to decide an initial and a final alpha for the animation.

We can also indicate if we want the actual state of the object to be the Final state of the animation, providing on the behavior only the first animation state. We can of course add both states, (initial and final). But, for the sake of displaying a not projected control with text (if it has any projection Silverlight 4.0 actually shows it blurred no matter what we do) this option has been added.
It is inspired trying to implement an easy way of defining & executing very quickly 3D animations  that respond to user actions.

It has been designed to be used together with the other 3D Behaviors, that have been published at the Expression Gallery, including this one:
• http://gallery.expression.microsoft.com/en-us/Project3DToMouse
• http://gallery.expression.microsoft.com/en-us/Slide3DToMouse
• http://gallery.expression.microsoft.com/en-us/MouseOver3D
• http://gallery.expression.microsoft.com/en-us/Loop3d
• http://gallery.expression.microsoft.com/en-us/Animate3D

You can also check how this and the other mentioned effects look used together here: http://www.bcndev.net (it is not published right now but will be in short…)
Also I would love to see places where this behavior(s) are being used or any design/implementation you might have done with them.

If you like the behavior, I’d be appreciated if you could vote some stars for this behavior, my thanks!

Have fun 🙂