Listbox datatemplate - an element is selected only by clicking on a sub-element, and not just anywhere on the element

I have a list with a data table for items. The problem is that selecting an item does not work by simply clicking anywhere on the item; I have to click on a specific subitem so that it really works.

My item has an image and a text block. If I hover over an image or text block, I actually see a hover effect. If I hover over any "empty" space of an element, there is no freezing effect (and no choice when I click there).

Image example: http://i33.tinypic.com/wvtleg.png

If I click (or hover) the actual text or image, it works fine, but if I find the mouse in empty areas (I drew a red line around it :)), the list does not respond.

How to get a freeze / click list to respond to a click anywhere in the item space?

For completeness, my Listbox + template is presented:

<ListBox Grid.Row="1" ItemsSource="{Binding Path=CreatableOutputWindows, Mode=OneWay}" Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="8,8,8,8" Name="listBox1" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"> <ListBox.ItemTemplate> <DataTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="84"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Border Margin="5" BorderBrush="Black" BorderThickness="2"> <Image Source="{Binding Path=Image}" Stretch="Fill" Width="80" Height="50" /> </Border> <StackPanel Grid.Column="1" Margin="5"> <StackPanel Orientation="Horizontal" TextBlock.FontWeight="Bold"> <TextBlock Text="{Binding Path=Name}" /> </StackPanel> </StackPanel> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox> 
+7
wpf hover listbox selection
source share
2 answers

As the Quartermeister pointed out - you need to set the grid background - but you also need to set the following style in your resources, since the default HorizontalContentAlignment ListBoxItems is set to Left. (It is not enough to install it in a ListBox)

 <ListBox.Resources> <Style TargetType="{x:Type ListBoxItem}"> <Setter Property="HorizontalContentAlignment" Value="Stretch"/> </Style> </ListBox.Resources> 
+7
source share

It looks like your ListBoxItem is not responding to hit testing . Try setting the grid background to transparent:

 <Grid Background="Transparent"> 

The default value is null, which will make the element invisible for testing. Setting the background to transparency does not affect the display, but allows you to delete the item. The default style for ListBoxItem will have a transparent background, but you may have reinstalled it.

+13
source share

All Articles