Blog

Books

Books I tech reviewed and recommend
Sorry I can't reply to every email and comment. If you need a fast response, try the WPF Forum or Silverlight Forum.
October 3, 2005

New feature in September PDC: DisplayMemberPath

As I’ve shown in previous posts, binding an ItemsControl to an IEnumerable data source is really easy (remember that ListBox and ComboBox derive from ItemsControl). With the introduction of DisplayMemberPath in September PDC, it became even easier for the scenario where you want to display only one property of each data item as text. Before DisplayMemberPath, this scenario required the use of a DataTemplate that would specify the property we’re interested in, like in the following xaml:

    <Window.Resources>
        <DataTemplate x:Key="itemTemplate">
            <TextBlock Text="{Binding Path=Name}" />
        </DataTemplate>
    </Window.Resources>
    
    <ItemsControl ItemsSource="{StaticResource greekGods}" ItemTemplate="{StaticResource itemTemplate}" />

The Data Binding team realized that this was a very common scenario and could be simplified, which was the motivation for introducing the DisplayMemberPath property in ItemsControl. The scenario above can now be done in a single line of xaml:

    <ItemsControl ItemsSource="{StaticResource greekGods}" DisplayMemberPath="Name" />

It’s that easy :)

The image below shows both versions of the ItemsControl, the one on the left is using DataTemplate and the one on the right is using DisplayMemberPath.

Here you can find the VS project with this sample code. This works with September CTP WPF bits.

Update September 18, 2007: Here you can find this project updated with Orcas Beta 2 bits.


Posted by Bea under WPF | Comments (4)

4 Responses to “New feature in September PDC: DisplayMemberPath”

  1. Matt Hamilton says:

    Hi Beatriz,

    I’ve noticed, at least in ListBox, that setting DisplayMemberPath enables keyboard lookups on the control. So if I start typing the first few letters of an item’s text, the selection jumps to that item.

    What’s especially useful is that this behaviour remains even if I also set ItemTemplate, so that my items can take on a customized appearance.

    However, I notice that Visual Studio in the background throws a tonne of these silent exceptions:

    System.Windows.Data Error: 20 : Both ‘ContentTemplate’ and ‘ContentTemplateSelector’ are set; ‘ContentTemplateSelector’ will be ignored.

    Is this ok? Or is there a recommended way to get the keyboard lookup behaviour while using ItemTemplate?

    Cheers,
    Matt

    • Bea says:

      Hi Matt,

      I actually get a compile error when setting both DisplayMemberPath and ItemTemplate with the Orcas build I have. You should never have to set both properties at the same time.

      If you want to enable search when ItemTemplate is set, you can set TextSearch.TextPath=”Name” on the ListBox.

      We enabled search by default with DisplayMemberPath because it’s really easy for us to know what text you want to use for the search. With ItemTemplate it’s a lot more complicated, so we only enable it when you specify the search path explicitly.

      Let me know if this helps.

      Bea

Leave a Reply

For spam filtering purposes, please copy the number 7523 to the field below: