Wpf ListBox控件觸發ListBoxItem點擊事件(接合Prism)

十年河東,十年河西,莫欺少年窮

學無止境,精益求精

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 並接收參數

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章