简体   繁体   English

在ListBoxItem上的ViewModel中触发一个事件,单击

[英]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. 我在查找用于将ListBoxItem上的click事件绑定到ViewModel中的属性的正确属性时遇到一些问题。

Code


Here is my ListBox XAML code: 这是我的ListBox XAML代码:

<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. 因此,当用户单击ListBox中的项目之一时。 I need an event to fire in my ViewModel. 我需要在ViewModel中触发一个事件。

1) With Interactivity and MVVM : 1)具有交互性和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 2)-或没有MVVM的样式

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

Use MouseLeftButtonUp event 使用MouseLeftButtonUp事件

 <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}/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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