Question

I'm trying to load a collection to listbox as follows.(one item in listbox contains a image and a text block) IF i click each item by item in outside of the image( area 2) selection change working fine(highlight correctly); but if do the selection change by clicking each image this want work correctly.What is the reason behind this?
enter image description here item by item

 <DataTemplate  x:Key="ObjectGalleryDataTemplate" DataType="{x:Type loc:ObjectTypes}" >
       <Button Margin="3" Width="80" Height="80" Click="click_object"BorderBrush="Transparent"  Tag="{Binding ObjectTypeID}">

<ItemsPanelTemplate x:Key="ObjectGalleryItemsPanelTemplate">

        <UniformGrid Rows="1"  HorizontalAlignment="Stretch"/>
        </ItemsPanelTemplate>

<StackPanel Orientation="Horizontal">
  <Grid><ListBox x:Name="ObjectTypesGallery" SelectionMode="Single" SelectionChanged="objectType_clik" BorderBrush="Transparent" SelectedIndex="0" ItemsSource="{Binding}" ItemTemplate="{DynamicResource ObjectGalleryDataTemplate}" ItemsPanel="{DynamicResource ObjectGalleryItemsPanelTemplate}">
      </ListBox>
   </Grid>
</StackPanel>
Was it helpful?

Solution

Since you asked:

what is the reason behind this?

The reason is that each item in a list box is automatically made selectable (so that you can choose one or more list items as part of your UI). But since you have included a button in your list item template, when you click on the button, the system is responding to the button being clicked first, in front of the list item.

You didn't specify how you want this to behave. I would guess that you should either use an <ItemsControl> if you don't want to be able to select individual items, or change your <Button> to an <Image> or something similar if you don't want to be able to click the icon in the center. Or, if you want both click events to fire (the button click, and the list item selection click), you will need to implement this manually using event handlers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top