0
votes

I'm trying to create a ListBox in which the text of the items is bound to the regionName property of the objects in its ItemSource. This is displaying as it should be, but clicking on the text created by the binding does not change the ListBoxItem's state to "selected" (although clicking on the space after the text selects the item.) The ListBox behaves as expected when I use the DisplayMemberBinding property or when I set Content to random static text in the DataTemplate (clicking on the text selects the ListBoxItem). However, I would like to use a converter to set the text color on the individual items (different items will be different colors), so it seems that I have to use a DataTemplate, which cannot be used in conjunction with the DisplayMemberBinding property. Has anyone else encountered this issue?

XAML for the list box:

            <ListBox x:Name="x_UpdateAreaListBox" Margin="0,0,0,10" SelectionChanged="x_UpdateAreaListBox_SelectionChanged" >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <ListBoxItem Content="{Binding Path=regionName}"/>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
2

2 Answers

2
votes

Replace the ListBoxItem inside the Datatemplate to TextBlock or ContentControl. ListBox itself has ListBox item gets generated, so the unnecessary ListBoxItem in your datatemplate eats up the mouse events, that is why you are seeing weired behaviour

2
votes

You'll want to use Label or TextBlock instead of ListBoxItem. The ListBox will automatically generate containers for your DataTemplated data.

See here for more about item container generation.