Mouse double click in Silverlight 2

by Scosby Wednesday, March 4, 2009

Mike Snow posted an excellent code snippet on how to implement your own Double Click functionality on his blog:

http://silverlight.net/blogs/msnow/archive/2009/01/15/silverlight-tip-of-the-day-82-how-to-implement-double-click.aspx

You could take his example and extend it by creating a new event in your own class, something like "Double Click" Wink

Tags: , ,

Technology | Programming

But Wait!! I want to respond to the space bar in my Silverlight 2 ListBox!

by Scosby Wednesday, March 4, 2009

Assume you are registered for the KeyDown event on a ListBox control that contains a collection of CheckBoxes. You want to make your users' lives easier by letting them hit the space bar to toggle the CheckBox.IsChecked property you're in for a surprise!

If you're looking for an explanation of why you can't handle the Key.Space enumeration. According to Microsoft, "ButtonBase implements OnKeyDown in such a way that the SPACE key invokes a button click, but also sets Handled, which prevents SPACE from being reported as a KeyDown event that could be handled by a button instance". Obviously, ButtonBase has nothing to do with a ListBox, but we can see that depending upon the implementation of the OnKeyDown event, a class might not allow a specific event bubble up if the Handled property is set to true. This appears to be the case with the ListBox class as well. Since I cannot think of a reason for a ListBox (NOT an item) to not respond to the space bar, I don't mind changing this behavior.

So how do you get a ListBox to respond to the space bar? Well, young grasshopper, you must follow the Silverlight 2 way of creating your own class to function the way the base classes should. Create a class derived from ListBox and override OnKeyDown. In your derived class implementation, do NOT call the base class implementation all the time. Instead, set handled to false if the space key was pressed, for all other keys you should call the base class implementation. This allows the arrow keys to continue to function as you would expect.

protected override void OnKeyDown(KeyEventArgs e)
{
     switch (e.Key)
     {
           case Key.Space:
                 e.Handled = false;
                 break;

           default:
                 base.OnKeyDown(e);
                 break;
     }
}

Tags: , ,

Technology | Programming

Good article for Silverlight 2: A UserControl base class for Visual State Manager

by Scosby Monday, February 9, 2009

The Expression Blend and Design team have posted a base class for building your own UserControl and utilizing the Visual State Manager on their blog. This makes it easy for you to define states for your control and transition between them when appropriate.

Read their article here.

Tags: , ,

Technology | Programming

Silverlight 2 and cross-browser compatibility when using HtmlTextWriter

by Scosby Tuesday, February 3, 2009

If you are using a custom ASP.NET web control to render your Silverlight 2 Object tag with the HtmlTextWriter, you need to make sure to render the Object tag's param elements as self-closing. Otherwise, you will discover that IE 6, 7, and 8 will ignore the invalid XHTML 1.0 Transitional "</param>" element and load the Object tag (meaning your Silverlight app) correctly but Google Chrome and Mozilla Firefox will not.

Instead, you should render your param elements as self-closing by using the HtmlTextWriter's WriteBeginTag method and the SelfClosingTagEnd constant. According to MSDN's WriteBeginTag documentation, "the WriteBeginTag method does not write the closing angle bracket (>) of the markup element's opening tag. This allows the writing of markup attributes to the opening tag of the element." This means you can close it yourself with the SelfClosingTagEnd constant, which they do not demonstrate but is easily to accomplish and removes the need to call WriteEndTag in their example.

This behavior can be verified easily. If you remove the self-closing tag from a Visual Studio Silverlight Web's *.html test page and add the explictly closed element, the behavior will manifest itself.

PS - Chrome and Firefox will also not load the Object tag if you provide a param element with an empty value. For example: <param name="onerror" value="" />

Tags: , , , ,

Technology | Programming