Set Interaction.Triggers to ListBoxItem

笑着哭i 提交于 2019-12-02 07:40:37

问题


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)


回答1:


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>



回答2:


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;

        }



回答3:


  <ListBox.Triggers>
                    <EventTrigger RoutedEvent="PreviewMouseDown">
                        <action:WorksheetListBoxAction />
                    </EventTrigger>
                </ListBox.Triggers>

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



来源:https://stackoverflow.com/questions/19488177/set-interaction-triggers-to-listboxitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!