开发者

DataTemplated ListBox items not responsive to mouse clicks

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>


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜