zoukankan      html  css  js  c++  java
  • Mvvm绑定datagrid或listview的selectItems的方法[转]

    单选,很简单,将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");
                            }
                        );
                }
            }
    
  • 相关阅读:
    文件的基本操作整理
    简谈深浅拷贝
    python进阶之内置方法
    python基础之打/解包及运算符与控制流程
    python中的常用数据类型
    python基础学习(起步)
    趣谈编程语言 (续集)
    豆瓣王守崑:大数据时代社交图谱与兴趣图谱的融合
    常见面试之机器学习算法思想简单梳理
    Future Research Directions in Social Recommendation
  • 原文地址:https://www.cnblogs.com/xiangfeideshui/p/3978637.html
Copyright © 2011-2022 走看看