Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add an event on ListBoxItems build with ItemTemplate

Tags:

wpf

xaml

I have a ListBox like this :

    <ListBox DataContext="{Binding UpdateSourceTrigger=PropertyChanged}"
             ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged}"
             ListBoxItem.Selected="ListBoxItem_Selected">
        <ListBox.ItemTemplate>
            <DataTemplate>
                   <StackPanel>
                        <DockPanel>
                            <Label Content="{Binding Path=Attribute[rdv].Value, UpdateSourceTrigger=PropertyChanged}" />
                        </DockPanel>
                        <DockPanel>
                            <Label Content="{Binding Path=Attribute[type].Value, UpdateSourceTrigger=PropertyChanged}" />
                            <Label Content="{Binding Path=Element[ville].Attribute[saisie].Value, UpdateSourceTrigger=PropertyChanged}" />
                            <Label Content=":" />
                            <Label Content="{Binding Path=Element[adresse].Attribute[saisie].Value, UpdateSourceTrigger=PropertyChanged}" />
                        </DockPanel>
                        <Separator />
                    </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

I whant to rise an event when an ListeBoxItem is selected.

As you can see, I've tried with ListBoxItem.Selected="ListBoxItem_Selected" but it does not work.

Do you have an idea ?

Tanks by advance !

like image 497
Service Informatique Avatar asked Aug 30 '10 09:08

Service Informatique


People also ask

How to add items to a ListBox in c# WPF?

On button click event handler, we add the content of TextBox to the ListBox by using the ListBox. Items. Add method. Now if you enter text in the TextBox and click Add Item button, it will add contents of the TextBox to the ListBox.

How to bind ListBox in WPF c#?

You can use data binding to bind data to the individual items. The following example shows how to create a ListBox that populates the ListBoxItem elements by data binding to a data source called Colors. In this case it is not necessary to use ListBoxItem tags to specify the content of each item.

What is ItemTemplate WPF?

You use the ItemTemplate to specify the visualization of the data objects. If your ItemsControl is bound to a collection object and you do not provide specific display instructions using a DataTemplate, the resulting UI of each item is a string representation of each object in the underlying collection.


1 Answers

You handler doesn't get called because the Selected event is already handled by the ListBox. You should handle the SelectionChanged event in the ListBox instead:

<ListBox DataContext="{Binding UpdateSourceTrigger=PropertyChanged}"
        ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged}"
        SelectionChanged="ListBox_SelectionChanged">

Alternatively, you can use an ItemContainerStyle to attach the handler to every ListBoxItem:

<ListBox DataContext="{Binding UpdateSourceTrigger=PropertyChanged}"
        ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged}">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <EventSetter Event="Selected" Handler="ListBoxItem_Selected"/>
        </Style>
    </ListBox.ItemContainerStyle>
like image 120
Quartermeister Avatar answered Sep 24 '22 00:09

Quartermeister