【问题标题】:fire an event in ViewModel on ListBoxItem click在 ListBoxItem 单击时触发 ViewModel 中的事件
【发布时间】:2016-03-11 10:23:48
【问题描述】:

问题


我在查找用于将 ListBoxItem 上的单击事件绑定到我的 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 中触发一个事件。

【问题讨论】:

    标签: c# wpf xaml mvvm listbox


    【解决方案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>
    

    【讨论】:

    • 小心,第一个选项还会监听列表框滚动条上的双击,这是你很可能不想要的。
    【解决方案2】:

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

    【讨论】:

    • 执行此操作时我似乎遇到了错误。我将绑定称为“SlideClick”,并在我的虚拟机中使用了以下代码:public ICommand SlideClick { get { return new DelegateCommand&lt;object&gt;(SlideClick_Click); } }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多