单选,很简单,将SelectedItem与ViewModel的属性进行双向绑定就OK了

多选,由于ListView的SelectedItems不能进行绑定,需要将ListView的SelectionChanged事件转换成命令绑定到ViewModel,同时将SelectedItems传递到ViewModel层

示例:
首先添加程序集引用System.Windows.Interactivity.dll

xaml
添加命名空间引用

HTML code
<UserControl ...
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
/>
HTML code
<ListView ItemsSource="{Binding Source={StaticResource CustomerGroups}}"
x:Name="dataGrid1" >
<ListView.GroupStyle>
<StaticResourceExtension ResourceKey="CustomerGroupStyle" />
</ListView.GroupStyle>
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn DisplayMemberBinding="{Binding DisplayName,Mode=OneWay}" Header="Name" />
<GridViewColumn DisplayMemberBinding="{Binding Email,Mode=OneWay}" Header="E-Mail" />
<GridViewColumn DisplayMemberBinding="{Binding TotalSales,Mode=OneWay, ConverterCulture=zh-CN, StringFormat=c}" Header="Total Sales" />
</GridView.Columns>
</GridView>
</ListView.View>
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding SelectionChangeCommand}" CommandParameter="{Binding SelectedItems,ElementName=dataGrid1}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ListView>

viewmodel:

C# code
        public RelayCommand<IList> SelectionChangeCommand
{
get
{
return new RelayCommand<IList>
(
(selectedItems) =>
{
TotalSelectedSales = 0; CustomerViewModel firstCustomer = null; if (selectedItems.Count > 0)
firstCustomer = selectedItems[0] as CustomerViewModel; // 将选中第一条CustomerViewModel传递到主页面
selectedCustomer = firstCustomer; // 更新按钮的可执行状态
EditCustomerCommand.RaiseCanExecuteChanged();
DeleteCustomerCommand.RaiseCanExecuteChanged(); foreach (dynamic Item in selectedItems)
TotalSelectedSales += Item.TotalSales; RaisePropertyChanged("TotalSelectedSales");
}
);
}
}

Mvvm绑定datagrid或listview的selectItems的方法[转]的更多相关文章

  1. WPF中TreeView控件SelectedItemChanged方法的MVVM绑定

    问题描述:左侧treeview控件中点击不同类别的节点时,右侧的页面会显示不同的权限.比如对于My Publications,拥有Modify和Delete两种权限,对于My Subscription ...

  2. “Win10 UAP 开发系列”之 在MVVM模式中控制ListView滚动位置

    这个扩展属性从WP8.1就开始用了,主要是为了解决MVVM模式中无法直接控制ListView滚动位置的问题.比如在VM中刷新了数据,需要将View中的ListView滚动到顶部,ListView只有一 ...

  3. .NET CORE(C#) WPF简单菜单MVVM绑定

    微信公众号:Dotnet9,网站:Dotnet9,问题或建议:请网站留言, 如果对您有所帮助:欢迎赞赏. .NET CORE(C#) WPF简单菜单MVVM绑定 阅读导航 本文背景 代码实现 本文参考 ...

  4. Silverlight实用示例 - DataGrid行详细信息的绑定DataGrid.RowDetailsTemplate

    Silverlight实用示例 - DataGrid行详细信息的绑定DataGrid.RowDetailsTemplate 2012-12-28 21:04 来源:博客园 作者:chengxingli ...

  5. WPF DataGrid与ListView性能对比与场景选择

    开门见山的说 性能对比: 在Demo中,DataGrid与ListView默认开启虚拟化(可以理解为动态渲染,类似懒加载只渲染屏幕可以看见的地方) DataGrid渲染10列50行随机字符280ms ...

  6. WPF DataGrid 自动生成行号的方法(通过修改RowHeaderTemplate的方式)

    WPF中的DataGrid自动生成行号的方法有很多,这里记录了一种通过修改 RowHeaderTemplate的方式来生成行号: 方法一: xaml界面: <Window ... xmlns:l ...

  7. ListView的getFirstVisiblePosition等方法返回的是哪个对象

    int firstPosition = lisView.getFirstVisiblePosition(); int lastPosition = lisView.getLastVisiblePosi ...

  8. android 修改listview item view 的方法(转)

    android 修改listview item view 的方法   具体的解答办法很简单: 代码如下 : 1.获取需要更新的view int visiblePosition = mListView. ...

  9. ListView单条刷新的方法

    我们一般会调用notifydatasetchange通知listView刷新界面.但会造成getView方法被多次调用(画面上能显示多少就会被调用多少次),如果是很明确的知道只更新了list中的某一个 ...

随机推荐

  1. C# Dictionary的xml序列化

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  2. Linux process state codes

    Here are the different values that the s, stat and state output specifiers (header "STAT" ...

  3. Jquery 进度条集锦

    http://sc.chinaz.com/tag_jiaoben/JinDuTiao.html?qq-pf-to=pcqq.group

  4. Linq to Object实现分页获取数据

    最后调用的CSharp代码可以是: int totalcount = dboperator.Customers.Count(); //总记录数 int pagesize = 100;//每页显示数 / ...

  5. MySql The service could not be started

    MySql安装 由于需要用mySql数据库今天就把它安上了,每次安装软件,数据库总是够我们折腾的,有时出现错误甚至比重装系统还要让人头疼. 今天在安的过程中就不出了很多错误,在重启与重装的反复捣鼓中终 ...

  6. 【2014】【辛星】【php】【秋季】【2】第一个php程序

    <span style="font-family:KaiTi_GB2312;font-size:18px;">*******************设置server** ...

  7. Web classPath

    classpath,看名字,类路径,这样比如,对于java程序,就是告诉java程序哪里去找类.(java虚拟机都是通过类装载器的)想myeclipse中struts,spring,hibernate ...

  8. Mysql新建用户和数据库并授权

    测试环境:Centos 6.3和Mysql 5.3 一.新建用户 //登录MYSQL@>mysql -u root -p@>密码//创建用户mysql> insert into my ...

  9. iOS--NSAttributedString使用介绍

    iOS–NSAttributedString使用介绍 原文见: http://www.itnose.net/detail/6177538.html http://***/html/topnews201 ...

  10. ios中的界面跳转方式

    ios中,两种界面跳转方式 1.NavgationController本身可以作为普通ViewController的容器,它有装Controller的栈,所以可以push和pop它们,实现你所说的跳转 ...