Community Blogs

Blogs of different SQL/Developers Community Members
Signed in as anonymous | Edit Profile | Sign out | Help
in Search

Weblog :: Boris Ševo

Sporadic posts about my interests, e.g. software development (mostly .NET), technology in general and some occasional rant.

listopad 2010 - Posts

  • Fix for the bug in ComboBox and ListBox WPF JetPack theme

    On Tim Heuer's blog you can find great Silverlight 4 JetPack theme. Thanks to Jonathan Antoine, JetPack is ported to WPF. Using JetPack theme in one of mine projects I found a tiny but very annoying bug, while I was trying to bind ComboBox and ListBox to collection of objects. The same bug is actually an old bug which could be found in some WPF Themes (Whistler Blue is one example).

    If you run the WPF JetPack theme sample code from Jonathan Antoine's blog you probably wouldn't notice this bug, because sample code binds ComboBox and ListBox to list of String objects. Bug can be reproduced only when those controls are binded to collection of objects which aren't strings.
    If you bind ComboBox and ListBox to collection of Dummy objects which have SomeText property and if you set ComboBox's DisplayMemberPath property to SomeText this is what you will get:

    screenshot-of-the-reproduced-bug 

    There are 2 solutions for this problem:

    1. override ToString method in binded object and let the ToString method return value of the property used in DisplayMemberPath
    2. modify theme's XAML code

    First solution is easy but it isn't practical in all cases (what if you need to override object's ToString method in some other way for some other purposes).
    Modifying theme's XAML code is actually very easy because all we have to do is replace lines 1840-1843 in Assets/CoreStyles.xaml:

    <ContentPresenter x:Name="contentPresenter" Content="{TemplateBinding Content}"
        ContentTemplate="{TemplateBinding ContentTemplate}"
        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
        Margin="{TemplateBinding Padding}" />

    with:

    <ContentPresenter x:Name="contentPresenter"
        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
        Margin="{TemplateBinding Padding}" />

    That's it. ComboBox will now bind and work as expected.
    ListBox I couldn't fix the same way - modifying the theme's XAML code. For the ListBox there is even simpler solution and that is to change the way you bind Dummy objects to ListBox. If you take a look at MainWindow.xaml sample code found at Jonathan Antoine's blog you will see that at line 67, ListBox is binded this way:

    <ListBox ItemsSource="{Binding ListOfItems}" Height="135.48" /> 

    ListOfItems is collection of Dummy objects and to bind them correctly and display values of the SomeText properties you need to use the following XAML code:

    <ListBox ItemsSource="{Binding ListOfItems}" Height="135.48" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding SomeText}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    

    Note that you couldn't fix the ComboBox the same way the ListBox is fixed. If you change CombBox's binding the same way I did for ListBox, Combox's items will display correctly but if you set SelectedIndex property to 0 you will see that binding doesn't work. The same will happen if you use SelectedItem instead of SelectedIndex.
    That's it, I hope this will save someone few minutes of work.

Powered by Community Server (Commercial Edition), by Telligent Systems