1 按照《prism搭建项目》搭建prism项目

2 修改MainWindow.xaml,引入behaviours命名空间,用该命名空间的Interaction,给listbox添加事件触发器

<Window x:Class="Prism.UseInvokeCommandAction.Views.MainWindow"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:Prism.UseInvokeCommandAction"xmlns:prism="http://prismlibrary.com/"prism:ViewModelLocator.AutoWireViewModel="True"xmlns:i="http://schemas.microsoft.com/xaml/behaviors"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><Grid.RowDefinitions><RowDefinition Height="100"/><RowDefinition /></Grid.RowDefinitions><ListBox ItemsSource="{Binding Items}"  SelectionMode="Single"><i:Interaction.Triggers><i:EventTrigger EventName="SelectionChanged"><prism:InvokeCommandAction Command="{Binding SelectedCommand}" TriggerParameterPath="AddedItems" /></i:EventTrigger></i:Interaction.Triggers></ListBox><StackPanel Grid.Row="1" Margin="5" Orientation="Horizontal"><TextBlock Foreground="DarkRed" FontWeight="Bold">Selected Item:</TextBlock><TextBlock AutomationProperties.AutomationId="SelectedItemTextBlock" Foreground="DarkRed" FontWeight="Bold" Margin="5,0" Text="{Binding SelectedItemText}" /></StackPanel></Grid>
</Window>

3 修改MainWindowViewModel

using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Prism.UseInvokeCommandAction.ViewModels
{public class MainWindowViewModel:BindableBase{private string _title = "Prism Unity Application";public string Title{get { return _title; }set { SetProperty(ref _title, value); }}private string _selectedItemText;public string SelectedItemText{get { return _selectedItemText; }private set { SetProperty(ref _selectedItemText, value); }}public IList<string> Items { get; private set; }public DelegateCommand<object[]> SelectedCommand { get; private set; }public MainWindowViewModel(){Items = new List<string>();Items.Add("Item1");Items.Add("Item2");Items.Add("Item3");Items.Add("Item4");Items.Add("Item5");// This command will be executed when the selection of the ListBox in the view changes.SelectedCommand = new DelegateCommand<object[]>(OnItemSelected);}private void OnItemSelected(object[] selectedItems){if (selectedItems != null && selectedItems.Count() > 0){SelectedItemText = selectedItems.FirstOrDefault().ToString();}}}
}

prism InvokeCommandAction相关推荐

  1. WPF的Prism框架简介

    什么是Prism Prism是一个用于在 WPF.Xamarin Form.Uno 平台和 WinUI 中构建松散耦合.可维护和可测试的 XAML 应用程序框架. Prism是一个用于在WPF.Xam ...

  2. WPF Prism框架

    Prism框架 1.关于Prism框架 ​ 官方地址:http://prismlibrary.com ​ 官方源码:https://github.com/PrismLibrary/Prism ​ 版本 ...

  3. WPF Prism(五)Navigation

    WPF Prsim(一)Region WPF Prism(二)Module WPF Prism(三)ViewModelLocator WPF Prism(四)MVVM WPF Prism(五)Navi ...

  4. 【wpf】DataGrid的使用

    DataGrid动态生成 简单方式 动态生成实现需要设置AutoGenerateColumns="True", <DataGrid Name="dataGrid&q ...

  5. WPF中对鼠标事件转换为命令处理模式的鼠标位置参数传递及EventArgs传递

    在WPF中使用命令模型,对于实现ICommandSource的元素来讲,比较方便,对于未实现ICommandSouce的元素来讲可以利用Blend中提供的行为来解决.通常情况下,命令对象可以传递命令参 ...

  6. 0613-prism-docs

    https://docs.microsoft.com/en-us/previous-versions/msp-n-p/ff648465(v%3dpandp.10) 依赖注入 使用Prism Libra ...

  7. WPF 控件【L】ListView(一) ListView如何实现单行SelectedItem或多行SelectedItems的绑定

    一.SelectedItem ListView的SelectedItem默认只能选择一行,那么我要绑定选中多行呢?怎么办? 二.SelectedItems 只能用Prism架构的命令方式,这种方式可以 ...

  8. C# WPF MVVM模式Prism框架从零搭建(经典)

    01 - 前言 目前最新的PRISM的版本是8.1.97,本节以6.3.0.0 讲解,可以在Github上获取PRISM的源码. Prism Github地址:https://github.com/P ...

  9. WPF开发学生信息管理系统【WPF+Prism+MAH+WebApi】(完)

    最近通过WPF开发项目,为了对WPF知识点进行总结,所以利用业余时间,开发一个学生信息管理系统[Student Information Management System].前四篇文章进行了框架搭建和 ...

最新文章

  1. OpenCV的cvLoadImage函数
  2. Openreach与华为/诺基亚签署G.fast协议
  3. linux下软件编译安装 前提和方式
  4. ViewPager+Fragment懒加载
  5. jzoj4208-线段树什么的最讨厌了【dfs】
  6. html和vue的区别,2018-09-16第四课 (v-html和v-text的区别,v-once和v-pre的区别,vue的生命周期,选项卡)...
  7. Vue基本指令(详细,好理解,示例代码)
  8. Qwins系统工具v1.3.0绿色版
  9. 仿映客直播礼物特效制作流程
  10. AI车牌识别涉及哪些技术?它是如何改变行业的?
  11. GTK、GDK、GLIB三者的关系 - 阿堂的专栏 - 博客频道 - CSDN.NET
  12. 畅通工程1863(并查集)(WA的思考)
  13. 简介响应状态码1xx、2xx、5xx
  14. Bagging...
  15. 结构方程模型的建立、拟合、评估、筛选和结果展示全过程
  16. 防疫新装备友倍康血氧仪
  17. 鹿鼎记 / Royal Tramp (1992)
  18. 由微软裁员产生的随想
  19. 买入开仓认购期权的风险有哪些 什么是买入平仓和买入开仓
  20. ADI Blackfin DSP处理器-BF533的开发详解24:触摸屏的实现和应用(含源代码)

热门文章

  1. 【微信小程序】小程序应用和页面生命周期
  2. SQL掌握:添加、修改、删除、查询4个句式
  3. 欧州专线的使用安全吗主要优势是什么
  4. 魅族16sPro更新体验版Flyme8,手机桌面更丰富,新增逆光自拍模式
  5. TCP/IP入门(2) --网络层
  6. 【毕业设计教程】单片机发送短信消息(GMS模块) - 物联网 嵌入式 stm32
  7. Android GMS Checklist【Android gms认证自检表】
  8. node 连接mongodb 1
  9. scrapy rule follow的理解和应用
  10. 时隔一年多,我胡汉三又归来了,将持续更新的博客