我在找到用于将ListBoxItem上的click事件绑定到ViewModel中的属性的正确属性时遇到了一些问题。
这是我的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>
因此,当用户单击ListBox中的一个项目时。我需要在我的ViewModel中触发一个事件。
答案 0 :(得分:1)
1)具有交互性和MVVM:
<ListBox>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding YourCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ListBox>
2) - 或者没有MVVM的风格
<Style TargetType="ListViewItem">
<EventSetter Event="MouseDoubleClick" Handler="ListViewItem_MouseDoubleClick"/>
</Style>
答案 1 :(得分:0)
使用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}/>