How to use ValueConverter in ListBox

I applied BenCon’s value converter in a ListBox.

==============  XML data source =================

<?xml version=”1.0″ encoding=”utf-8″ ?>
<players>
<player>1</player>
<player>2</player>
<player>3</player>
<player>4</player>
</players>

==============   XAML   ======================

<Page.Resources>
<local:PositionConverter x:Key=”PositionConverter”/>
<XmlDataProvider x:Key=”XMLDataSource” d:IsDataSource=”True” Source=”results.xml”/>
<DataTemplate x:Key=”DataTemplate1″>
<TextBlock Text=”{Binding Converter={StaticResource PositionConverter}, XPath=.}”/>
</DataTemplate>
</Page.Resources>
<Grid Background=”#FF000000″>
<ListBox ItemTemplate=”{StaticResource DataTemplate1}” ItemsSource=”{Binding Mode=Default, Source={StaticResource XMLDataSource}, XPath=/players/player}” Margin=”51,28,149,72″>
</ListBox>
</Grid>

===========================================

Actually it was a very simple application. The thing troubled me a lot was that I forgot how to do data binding in the TextBlock of DataTemplate1.   After the ListBox is databound to /players/player, each of TextBlock’s data context is the “player” XML element. Now I just needed use “.” to refer the current “player”. However, I forgot the “.”.  Therefore, even though it looks trivial, I want to write it down to remind me in case I make the similar mistake.

Advertisements

~ by Martin on September 9, 2007.

One Response to “How to use ValueConverter in ListBox”

  1. Hello, I am wondering if you have a working solution you can share with. I am trying to createe a value converter which will convert frame source Uri to itemsource index of the listbox item. I need to be able to change selection state of listbox item based on what is loaded into frame. I also define all content in XML. Thank you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: