[英]fire an event in ViewModel on ListBoxItem click
我在查找用於將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中觸發一個事件。
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>
使用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.