What I Learned In WP7 – Issue 16

Yesterday, I had a developer reach out to me about a seemingly simple problem, but it wasn’t until I looked at it in a greenfield project that I could solve it quickly.  In the XAML below, I have a ListBox that I bind a List of objects to.  What the developer was trying to do was get the text in the “AlternateText” TextBox for the item clicked.

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
	<ListBox x:Name="masterList" SelectionChanged="masterList_SelectionChanged">
				<StackPanel Orientation="Horizontal">
					<Image x:Name="ActionImage" Source="{Binding ActionImage}" Width="100" Height="100"/>
						<TextBlock x:Name="ActionText" Text="{Binding ActionText}" FontSize="40" Width="300" />
						<TextBlock x:Name="AlternateText" Text="{Binding ActionDescription}" />

The important thing to remember here is that when you call the SelectionChanged event on a ListBox, the ListBox still recognizes each item as its original object.  Therefore, you should actually be trying to access the specific object that was selected, and then access the properties of that object, instead of the XAML elements that make up the ListBox’s layout.

Here’s the solution we implemented, where TestClass is the object type we’re using:

private void masterList_SelectionChanged(object sender, SelectionChangedEventArgs e)
	ListBox testList = sender as ListBox;
	TestClass testText = (TestClass)testList.SelectedItem;
	string whatImLookingFor = testText.AlternateText;

3 thoughts on “What I Learned In WP7 – Issue 16

  1. I'm new to Silverlight and C# development, so I could be way off here, but since he's got the ListBox bound to a view model, couldn't he have just used that to get the text?

    private void masterList_SelectionChanged(object sender, SelectionChangedEventArgs e)
    if( e.AddedItems.Count > 0 ) {
    ListBoxItem item = e.AddedItems[0] as ListBoxItem;
    MyDataObject obj = item.DataContext;
    string altText = obj.ActionDescription;

    Of course, if he wants to get to user input text from the TextBox, the binding mode would have to be set to TwoWay.

  2. Usually it is best to make sure there is actually a selected item before trying to access it. If you have any code that un-select items the method might be called with SelectedItem = null. You can check for that case using:

    if(testList.SelectedIndex != -1){…}

    When using a ListPicker the UIElement itself might even be null in certain cases, so you should use:

    if (lpTest != null && lpTest.SelectedIndex != -1){…}

Leave a Reply to chaitanya venneti Cancel 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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s