1
votes

In my wpf application when I select listboxItem, SelectionChanged event of listbox is not firing. However event fires when I click on outer margin. have a look at below snap.

enter image description here

so basically, when I click on section inside Red border(Right Image), Selection change event is not firing, but when I click on outer border (White color part), selection change fires.

While searching about issue, I am not sure but I found it may be issue due to event tunneling. However I have only a bit of knowledge about tunneling yet.

So can any one please help me how this can get working so that selection change fire when I click on listboxitem (Red section)

let me if I need to further clear question. I am also putting listbox code here

<ListBox x:Name="Listbox1" SelectionChanged="listBox1_SelectionChanged">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <ListBoxItem Margin="10" Content="{Binding Name}" Height="25" 
                                             BorderBrush="#FF404040" BorderThickness="0,0.25" />
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

Thanks in anticipation

1

1 Answers

4
votes

I can't think of a reason why you would want to have a ListBoxItem inside the DataTemplate of a ItemTemplate. The ListBoxItems are genarated automatically for each element of the ListBox and whatever you have in your DataTemplate will be used as the content of that ListBoxItem So in your case you end up having a ListBoxItem inside a ListBoxItem. This might be the cause.

Try it this way:

<ListBox x:Name="Listbox1" SelectionChanged="listBox1_SelectionChanged">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Label Margin="10" 
                   Content="{Binding Name}" 
                   Height="25"
                   BorderBrush="#FF404040" 
                   BorderThickness="0,0.25" />
         </DataTemplate>                        
    </ListBox.ItemTemplate>
</ListBox>