GL.NET Group Meeting

|

The first Gloucestershire .NET User Group meeting date and agenda has been set (well done Jimmy). There will be two presentations: What’s new in C# 4.0 by Guy Smith-Ferrier, and the second by me on Sockets in Silverlight 2. The idea for my presentation is to code up a simple Silverlight 2 socket based application in front of your very eyes, Quick Start Guide style.

It should be a fun evening; would be great to see you there.

MIX09 10K Smart Coding Challenge

|

Update: Sadly all good things must come to an end; the MIX09 event is now over; I do not know how long they’ll keep the 10K completion pages alive for so I have uploaded my entry to the Silverlight Streaming Service (which is awesome BTW) and provides a simple page to host my entry: http://code.compilewith.net/pj.fviz.html 

–PJ


logo My entry for the 10K coding challenge has been accepted, let the voting begin.

The basic premise for my idea is to visualize RSS or ATOM feeds. I don’t know about you but I have loads of feeds in my and I come in to contact with countless others during my day; sometimes I just want a quick fingerprint of what the feed looks like, a snapshot of what they talk about, how often they post over what time frame etc. etc.

To that end the Visualizer was born:

Above is a screenshot of the application, showing three feeds: this blog, Derek’s Cloudstore and the feed for Scott Hanselman’s site. The following is the help for the application that describes each element within the UI:

Also, I’d like to point out the crux of the challenge: this has to all be achieved in 10K or less of source code. Source code includes all .cs and .xaml files as well as any resources and/or images used; a challenge indeed.

I would really appreciate it if you would take the time and pop on over to my entry and vote… I’m not asking for 5 stars or anything (although that would be nice :o), I’m just after your vote. As always if you have any other comments or suggestions please let me know.

ScrollViewer customization – Part 2

|

Carrying on from my previous post, I’ve added a little more functionality to the CyclicScrollViewer control. I have added code to handle horizontal scrolling and included an attached property for turning off the cyclic behaviour, making the control behave similar to a standard ScrollViewer . I have also created a sample application to show all these new features:

CyclicListBox

For the top two ListBoxes cyclic scrolling is enabled; so, if you click and hold the ScrollBar buttons the list goes around and around as you would expect. However, for the bottom ListBox the CyclicScrollViewer is in use, but the  CanContentCycle attached property has been set to False (the default is True).

Here’s the code for the attached dependency property:

public static readonly DependencyProperty CanContentCycleProperty =
    DependencyProperty.RegisterAttached(
        "CanContentCycle",
        typeof(bool),
        typeof(CyclicScrollViewer),
        new FrameworkPropertyMetadata(true) { Inherits = true });

The reason I wanted to show this code is that there is a gotcha when working with attached properties in this way: by that I mean when you apply them to parent containers as opposed to child objects (you apply a DockPanel’s attach properties to the elements it contains, as opposed to the containing element). In our case, we’re contained rather that containing. Anyway, ensure you replace the property metadata, by using a FrameworkPropertyMetadata instance, and set the Inherits to true; otherwise all you’ll see at runtime is the default value, no matter what you’d applied it in the code. Here’s how to use the property:

         l:CyclicScrollViewer.CanContentCycle="False" />

What you may notice if you download the sample application is that we’re still missing one key feature… keyboard support. Currently items only cycle if you use your mouse on the ScrollBar buttons, so that’s the next feature.

Update: Here's an XBAP version of the sample application for your viewing pleasure.

ScrollViewer customization

|

Another StackOverflow question; this time about making a ListBox cycle its contents when scrolling. The question:

WPF ListBox: Can you make it cycle? i.e. not hit hard stops at top and bottom

Seems like a pretty simple request: make a circular list, i.e. when you roll off the end of the list start from the beginning again (or end depending on which direction you scrolled in). After a quick examination of the control template for a ListBox I figured that a good solution might be to try and work with the ScrollViewer control, and see if I could get that to cycle... then I could simply replace the ScrollViewer instance in the control template for the ListBox, job done!

So that's what I did:

public class CyclicScrollViewer : ScrollViewer
{
    public CyclicScrollViewer()
    {
      this.CommandBindings.Add(new CommandBinding(ScrollBar.LineUpCommand, LineCommandExecuted));
      this.CommandBindings.Add(new CommandBinding(ScrollBar.LineDownCommand, LineCommandExecuted));
    }

    private void LineCommandExecuted(object sender, ExecutedRoutedEventArgs e)
    {
      if (e.Command == ScrollBar.LineUpCommand)
      {
          if (this.VerticalOffset == 0)
              this.ScrollToEnd();
          else
              this.LineUp();
      }

      if (e.Command == ScrollBar.LineDownCommand)
      {
          if (this.VerticalOffset == this.ScrollableHeight)
              this.ScrollToTop();
          else
              this.LineDown();
      }
   }
}

This code simply installs event handlers for the LineUpCommand and LineDownCommand commands dispatched by the vertical ScrollBar in the control template for the ScrollViewer. Which, by the way, is a seriously fabulous way for this control to behave. In the event handler I examine incoming command type, then dispatch to the LineUp or LineDown methods as appropriate. However, before that I do a check to see if the control is already fully scrolled to either the top or the bottom of the viewer, and then, depending on the command, call either ScrollToEnd or ScrollToTop to loop the ScrollViewer - all the methods used, and more, already exist on the ScrollViewer, making the coding a very simple affair.

I've uploaded a sample application that uses the new ScrollViewer here:

PaulJ.CyclicListBox.zip

This sample is clearly incomplete; it only handles vertical scrolling. However, you can see how it could be extended to handle horizontal scrolling too. In fact, I intend to hack on this a little more in the coming weeks and try and create a complete working control... unless another question catches my eye in the meantime ;)

I may also look into providing a Silverlight version, but that'll largely depend on the comments get back from this post, so let me know if you think that'll be a good idea.

As always, comments and suggestions very welcome.

Newer Posts Older Posts Home