Ryan Archibald
Ryan Archibald

Reputation: 215

Listbox highlight item

I am wondering why I can select an item in a listbox but not on the text. So I can select the item in the white space but when I click on the text or image it doesn't highlight and I am wondering why?

<Popup IsOpen="{Binding IsChecked, ElementName=toggleButtonAdd}" x:Name="Popup" StaysOpen="False" Placement="Right">
                <Border BorderBrush="Black" BorderThickness="1" Background="Gainsboro">
                    <StackPanel Margin="5,10,5,5">
                        <TextBlock Text="Add Existing Property:"></TextBlock>
                        <ListBox BorderBrush="Black" BorderThickness="1" Background="White" Margin="5" Padding="4" Width="130"
                                 ItemsSource="{Binding Path=AvailableProperties}" ><!--SelectionChanged="Selector_OnSelectionChanged">-->
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <ListBoxItem Margin="2">
                                        <StackPanel Orientation="Horizontal">
                                            <Image Source="{Binding Icon, Converter={StaticResource ImageToSourceConverter}}" Width="12" Height="12" Margin="3" VerticalAlignment="Center"/>
                                            <TextBlock Text="{Binding Name}" VerticalAlignment="Center"></TextBlock>
                                        </StackPanel>
                                    </ListBoxItem>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </StackPanel>
                </Border>
            </Popup>

Upvotes: 0

Views: 74

Answers (1)

netaholic
netaholic

Reputation: 1385

First of all, you don't need to add ListBoxItem to DataTemplate

If you remove ListBoxItem from DataTemplate the selection will work as expected. I really guessing here, but I think it has something to with the ListBox itself and the way it finds its children. It certainly not connected to the fact, that ListBoxItem is a ContentControl, since changing your example to ContentControl will fix an issue

Upvotes: 1

Related Questions