列表框突出显示项目

时间:2015-09-24 08:42:12

标签: wpf xaml listbox

我想知道为什么我可以选择列表框中的项目而不是文本。所以我可以选择白色空间中的项目,但是当我点击文本或图像时它没有突出显示,我想知道为什么?

<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>

1 个答案:

答案 0 :(得分:1)

首先,您无需将ListBoxItem添加到DataTemplate

如果您从ListBoxItem移除DataTemplate,则选择将按预期工作。 我真的在这里猜测,但我认为它与ListBox本身以及它找到它的孩子的方式有关。它肯定与ListBoxItem是ContentControl的事实无关,因为将您的示例更改为ContentControl将解决问题

相关问题