GL.NET WPF Talk

|

For those of you who attended my WPF talk last night at the GL.NET User Group meeting, thank you for coming and listening; here are the book references I promised:

1. Essential WPF by Chris Anderson

Chris was one of the Architects on WPF and therefore provides a lot of insight into why things were done the way they were. This book gives you the essence of what WPF is all about.

2. WPF Unleashed by Adam Nathan

Adam is an excellent writer and this all colour book is a real "how to" book; a book on how to get things done by using WPF.

3. Programming WPF by Chris Sells and Ian Griffiths

This is the deep dive on WPF, the nuts and bolts, the nitty gritty of how things work. This is a great book that I constantly find myself going back to.

If you have any questions or comments please feel free to drop me a line here, just add a comment to this post or send me a mail by using the contact link above. See you all next month at the next meeting; think deeply and enjoy.

Download the presentation.

MIX10 10K Smart Coding Challenge

|

logo The Microsoft MIX conference is starting to ramp up again with MIX10, and with it the MIX10K Smart Coding Challenge. So it’s time to crank out another application with 10K or less of source code, and get me another one of those lovely MIX t-shirts!

This year I’m doing something a little more conventional than my entry last year: a game (albeit a simple one), a game I like to call Bing Pairs. The idea is to simply find the pairs of Bing background images as fast as you can [click here to vote for my efforts].

Here’s a short video of the game in action:

All that, and all for less than 10K of source code!

I would be very grateful if you could take a moment out of your day to go and vote for my entry, thank you.

WPF Data Bound RadioButtonList

|

During the process of writing a WPF application recently, I had the need for a data bound list of items where the options had to be mutually exclusive, so I figured something like a list of RadioButtons would be in order.

However, when I started looking around I could not find a RadioButtonList or anything that fitted the bill out-of-the-box. Therefore, I thought I’d put something together myself; I also needed to have the list render horizontally rather than vertically. This is what I came up with:

<!-- Item Style for the ListBoxItem to add a RadioButton -->
<Style x:Key="RadioButtonItemStyle" TargetType="{x:Type ListBoxItem}">
    <Setter Property="Margin" Value="0,0,5,0" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <Border BorderThickness="0" Background="Transparent">
                    <!-- Note: IsChecked is bound to IsSelected-->
                    <RadioButton
                        Focusable="False"
                        IsHitTestVisible="False"
                        IsChecked="{TemplateBinding IsSelected}">
                        <ContentPresenter />
                    </RadioButton>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<!-- Turns the ListBox in to a Horizontal ListBox -->
<ItemsPanelTemplate x:Key="HorizontalItemsPanel">
    <VirtualizingStackPanel
        Orientation="Horizontal" />
</ItemsPanelTemplate>

I defined a Style and the ItemsPanelTemplate in the Resources property of my Window that contains all the necessary XAML for the effects I need; note that you could just as easily define this XAML inline on the ListBox as opposed to using the Resources property. I then applied these two new elements to an instance of ListBox using the following mark-up:

<ListBox
    BorderThickness="0"
    ItemsSource="{Binding MyDataList}"
    SelectedValue="{Binding MyDataListSelectedValue}"
    ItemContainerStyle="{StaticResource RadioButtonItemStyle}"
    ItemsPanel="{StaticResource HorizontalItemsPanel}" />
  

I’ve highlighted where I’ve used the two resources.

For this example the data list is trivial and is provided by a ViewModel class attached to the DataContext of the Window; I’m only showing the code here as a example of how you can add sample data to an application, as well as proving that the above XAML all works as expected using a bound list of data:

public class MainWindowViewModel
{
    public IEnumerable<string> MyDataList
    {
        get
        {
            yield return "Stan";
            yield return "Cartman";
            yield return "Kenny";
            yield return "Karl";
        }
    }

    public string MyDataListSelectedValue
    {
        get { return "Cartman"; }
        set { /* TODO: save the value */ }
    }
}

<!-- XAML -->
<Window.DataContext>
    <vm:MainWindowViewModel />
</Window.DataContext>

Here’s what it all looks like in the Visual Studio designer:

image

And there we have it, job done. Hope this helps someone, happy XAML hacking.