WindowsClient.NET

I’ve been following the WindowsClient.NET blog for some time now and have been really impressed with some of the content. In addition to some great tutorial videos on the site itself there have been some really good links to code fragments – such as:

Great content – great feed.

Sneak Peak

My little hobby app that I’ve been tinkering with now for over a year is finally beginning to take shape. The user interface is still a shambles – mainly because its my test bed for all the new WPF goodies I’ve been learning, but the database and data access layer are pretty much sorted out (thanks to LINQ). I’ve started importing a little data and was kinda happy with some of the initial results… so much so I just had to post a screenshot.

Sneak Peak 1

Hopefully, I’ll get a bit more time to work on this in the coming months and will be able to post something more interesting. Not the least of which being the stumbling blocks I’ve hit porting bits of this from WinForms to WPF and from my own hand-coded ORM/offline sync data layer to the equivalent in VS 2008…

Oh and completely off topic if you’ve ever wondered about the truth of what life is really like working at Microsoft then you should check out this link . I guess whilst I’m on the topic of amusing vids I also enjoyed Scott Hanselman and Chris Sells arguing about whether less is really more when it comes to home theatre and remotes on the first instalment of Geek Developer Cribs.

Styling a ListBox

I’ve found that the WPF ListBox control is one of my most commonly used controls when designing screens. Its ability to use DataTemplates to control how each item is rendered, ability to override the layout mechanism of the items via the ItemsPanelTempalte and even some pretty cool grouping capabilities.

Over the last couple of weeks I’ve put together a few screens in a little app that I’m writing. It had got to the stage where data was appearing on screen and the back-end (consisting of LINQ for SQL) was doing a good job of extracting the information from the existing database. I figured it was time to allow myself a little time to “pretty up” the UI.

I applied some very simple backgrounds and a simple animation for “mouse over” highlighting of list box items. But then I hit a stumbling block. The SelectedItem was being rendered using the standard blue background, or gray when the control didn’t have focus. What’s more this was taking preference to any property changes I made in the IsSelected trigger. No problem – that should be easy to change right? Hmm… at least that’s what I thought.

ListBoxItem width default selection 

What’s with the ugly blue background on the selected item?

Of course the most obvious choice seemed to be to completely redo the control template. Now apparently you can rip the template of a control using Expression (or code) and edit just the bit you want – but this sounds pretty horrid to me. It’s the equivalent of copying huge amounts of code that you don’t really understand and changing just a couple of lines – to achieve a simple effect.

The solution (to this common problem) that seemed to be the most prevalent on the net was certainly much easier, but it just seemed to be clumsy and error prone. You “highjack” the resource keys used to render the standard template’s selection background in a local scope – either replacing with your own colour – or disabling by making them transparent and handle the rendering yourself via your own triggers (with animations etc.).

        <ListBox x:Name="eventList" Background="Black" ItemsSource="{Binding Path=Events/Events}" 
IsSynchronizedWithCurrentItem
="True" Grid.Row="1" ItemTemplate="{StaticResource dayTemplate}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" BorderThickness="0"> <ListBox.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/> <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/> </ListBox.Resources> </ListBox>

For this to work:

  1. You have to know that the standard colours for highlighting in a ListBox are System.Highlight (fairly obvious) and System.Control (guesswork).
  2. No other components of the controls standard rendering that you want to retain can use these colours.

There must be a better, simple way?