简体   繁体   中英

fire an event in ViewModel on ListBoxItem click

Issue


I am having some problems finding the correct property to use to bind a click event on a ListBoxItem to a property in my ViewModel.

Code


Here is my ListBox XAML code:

<ListBox dd:DragDrop.IsDragSource="True" dd:DragDrop.IsDropTarget="True" Background="#ececec" SelectedItem="{Binding SelectedSlideDataItem}" ItemsSource="{Binding SlideDataItems}" Grid.Column="2" Grid.Row="10" Grid.RowSpan="4" Grid.ColumnSpan="13">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <!--<Setter Property="Padding" Value="5,0,5,5" />-->
                    <Setter Property="Height" Value="110"/>
                    <Setter Property="Width" Value="200"/>
                    <Style.Triggers>
                        <Trigger Property="Control.IsMouseOver" Value="True">
                            <Setter Property="ToolTip">
                                <Setter.Value>
                                    <Image Source="{Binding BackgroundImage}" Height="240" Width="400" />
                                </Setter.Value>
                            </Setter>
                            <Setter Property="Control.Background" Value="#d64b36" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#c83a25" />
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#c83a25" />
                <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="#c83a25"/>
            </ListBox.Resources>
        </ListBox>

So when a user clicks one of the items in the ListBox. I need an event to fire in my ViewModel.

1) With Interactivity and MVVM :

<ListBox>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseDoubleClick">
                <i:InvokeCommandAction Command="{Binding YourCommand}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </ListBox>

2) - Or in Style without MVVM

 <Style TargetType="ListViewItem">
        <EventSetter Event="MouseDoubleClick" Handler="ListViewItem_MouseDoubleClick"/>
    </Style>

Use MouseLeftButtonUp event

 <ListBox dd:DragDrop.IsDragSource="True" dd:DragDrop.IsDropTarget="True" 

Background="#ececec" SelectedItem="{Binding SelectedSlideDataItem}" ItemsSource="

{Binding SlideDataItems}" Grid.Column="2" Grid.Row="10" Grid.RowSpan="4"

 Grid.ColumnSpan="13" MouseLeftButtonUp={//Bind with VM}/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM