2

I have set Interaction.Triggers to ListBox and perform respective TargetedTriggerAction when 'SelectionChanged' event occurs, like below.

<ListBox x:Name="WorksheetListBox" ItemsSource="{Binding WorkSheetCollection}"
                             ItemTemplate="{StaticResource workSheetTemplate}">                              
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="SelectionChanged">
        <action:WorksheetListBoxAction />
    </i:EventTrigger>
  </i:Interaction.Triggers>
</ListBox>

But my requirement is I need to set Interaction.Triggers to ListBoxItem's 'PreviewMouseDown' event(Note: ListBox populated via ItemsSource)

Dinesh Kumar P
  • 1,128
  • 2
  • 18
  • 32
  • Hi Omribitan, If I replace "SelectionChanged" to "PreviewMouseDown", it triggers only for ListBox, not for ListBoxItem. I need to set this for ListBoxItem. So please suggest where I have to set this 'Event Trigger' for ListBoxItem – Dinesh Kumar P Oct 21 '13 at 07:15

3 Answers3

4

you can do it the PreviewMouseDown event on the ListBoxItem

<ListBox ItemsSource="{StaticResource Data}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Button Name="TaskButton" Content="{Binding}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <EventSetter Event="PreviewMouseDown"
                                 Handler="ItemOnPreviewMouseDown" />
                </Style>
            </ListBox.ItemContainerStyle>
        </ListBox>

        private void ItemOnPreviewMouseDown(
            object sender, MouseButtonEventArgs e)
        {

            ((ListBoxItem) sender).IsSelected = true;

        }
Debashrita
  • 940
  • 1
  • 8
  • 20
0

You can try something like this:

    <Style TargetType="{x:Type ListBoxItem}">
        <Style.Triggers>
            <EventTrigger RoutedEvent="PreviewMouseDown">
                <EventTrigger.Actions>
                    <action:WorksheetListBoxAction />
                </EventTrigger.Actions>
            </EventTrigger>
        </Style.Triggers>
    </Style>
Omri Btian
  • 6,499
  • 4
  • 39
  • 65
  • 1
    How could this work and even marked as the accepted answer? For starters, `action:WorksheetListBoxAction` is a `System.Windows.Interactivity.TriggerAction`, which cannot be used as the action of a `EventTrigger` inside `Style.Triggers`. It's also impossible to rewrite the action class to derive from `System.Windows.TriggerAction`, which would work in this scenario but unfortunately does not have a public constructor to derive from. – hillin May 31 '19 at 01:33
-1
  <ListBox.Triggers>
                    <EventTrigger RoutedEvent="PreviewMouseDown">
                        <action:WorksheetListBoxAction />
                    </EventTrigger>
                </ListBox.Triggers>

You can do the same without use of Interactivity.dll for event handling.

saurabh.mridul
  • 177
  • 1
  • 11