Wpf ListBox控件触发ListBoxItem点击事件(接合Prism)

发布时间 2023-12-14 17:03:35作者: 天才卧龙

十年河东,十年河西,莫欺少年穷

学无止境,精益求精

1、先贴出源码

xaml

<Window x:Class="WpfApp6.Views.MainView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp6.Views"
        xmlns:behavior="http://schemas.microsoft.com/xaml/behaviors"
        xmlns:Prism="http://prismlibrary.com/"
        Prism:ViewModelLocator.AutoWireViewModel="true"
        mc:Ignorable="d"
        Title="MainView" Height="450" Width="800">
    <Grid>
        <ListBox x:Name="menubar"  ItemsSource="{Binding menuBars}">
            <behavior:Interaction.Triggers>
                <behavior:EventTrigger EventName="SelectionChanged">
                    <behavior:InvokeCommandAction Command="{Binding menubarCommand}" CommandParameter="{Binding ElementName=menubar,Path=SelectedItem}"/>
                </behavior:EventTrigger>
            </behavior:Interaction.Triggers>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Border Background="{Binding menuColor}"/>

                        <TextBlock Text="{Binding menuName}" Foreground="{Binding menuForeColor}"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

    </Grid>
</Window>
View Code

viewModel

using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WpfApp6.Event;
using WpfApp6.UserControls;
using WpfApp6.Views;

namespace WpfApp6.ViewModels
{
    public class MainViewModel : BindableBase
    {
        /// <summary>
        /// 窗体Button按钮事件,用于点击后,弹出框
        /// </summary>
        public DelegateCommand<MenuBarDto> menubarCommand { get; set; }
        public ObservableCollection<MenuBarDto> menuBars { get; private set; }

        public MainViewModel()
        {
            menubarCommand = new DelegateCommand<MenuBarDto>(open);
            menuBars = new ObservableCollection<MenuBarDto>() { new MenuBarDto() { menuName = "我的购物车", menuColor = "Red", menuForeColor="Yellow" }, new MenuBarDto() { menuName = "我的订单", menuColor = "Pink", menuForeColor = "Red" }, new MenuBarDto() { menuName = "我的评价", menuColor = "Yellow", menuForeColor = "Green" } };
        }

        private void open(MenuBarDto obj)
        {
            MessageBox.Show($"菜单名称:" + obj.menuName);
        }
 
    }

    public class MenuBarDto
    {
        public string menuName { get; set; }
        public string menuColor { get; set; }
        public string menuForeColor { get; set; }
    }
}
View Code

效果:

 2、说明

关于绑定ListBox 及 Command 命令等相关说明,在此不再说明,主要说明事件触发器

2.1、引入命名空间

        xmlns:behavior="http://schemas.microsoft.com/xaml/behaviors"
        xmlns:Prism="http://prismlibrary.com/"
        Prism:ViewModelLocator.AutoWireViewModel="true"

引入了 prism 命名空间 及 prism 自动绑定 viewModel 

引入 behaviors 命名空间,为 ListBox EventTrigger 做准备

2.2、定义ListBox EventTrigger 并 绑定Command ,传递参数

 2.3 代码

 public DelegateCommand<MenuBarDto> menubarCommand { get; set; }

定义Command 并接收参数