zoukankan      html  css  js  c++  java
  • WPF mvvm select multi items via

    1.install-package Microsoft.Xaml.Behaviors.Wpf;

    2.xmlns:i="http://schemas.microsoft.com/xaml/behaviors";

    3.

    <DataGrid x:Name="dg" Grid.Row="0" ItemsSource="{Binding DataList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
    AutoGenerateColumns="False" SelectionMode="Extended"
    Width="Auto" Height="Auto" HorizontalAlignment="Center" VerticalAlignment="Center">
    <i:Interaction.Triggers>
    <i:EventTrigger EventName="SelectionChanged">
    <i:InvokeCommandAction Command="{Binding SelectionChangedCommand}" CommandParameter="{Binding ElementName=dg,Path=SelectedItems}"/>
    </i:EventTrigger>
    </i:Interaction.Triggers>

    4.In MVVM,

    private ObservableCollection<SalesOrderDetail> selectedItems;
    public ObservableCollection<SalesOrderDetail> SelectedItems
    {
    get
    {
    return selectedItems;
    }
    set
    {
    if(value!=selectedItems)
    {
    selectedItems = value;
    OnPropertyChanged("SelectedItems");
    }
    }
    }

    private DelegateCommand selectionChangedCommand;
    public DelegateCommand SelectionChangedCommand
    {
    get
    {
    if(selectionChangedCommand==null)
    {
    selectionChangedCommand = new DelegateCommand(SelectionChangedCommandExecuted);
    }
    return selectionChangedCommand;
    }
    }

    private void SelectionChangedCommandExecuted(object obj)
    {
    var selectedOrders=new List<SalesOrderDetail>((obj as IList).Cast<SalesOrderDetail>());
    if(selectedOrders!=null && selectedOrders.Any())
    {
    SelectedItems = new ObservableCollection<SalesOrderDetail>(selectedOrders);
    }
    }

  • 相关阅读:
    Python爬虫教程-06-爬虫实现百度翻译(requests)
    Python爬虫教程-04-response简介
    Python爬虫教程-05-python爬虫实现百度翻译
    LeetCode——Balanced Binary Tree
    LeetCode——Min Stack
    LeetCode——Count and Say
    LeetCode——Invert Binary Tree
    LeetCode——Contains Duplicate II
    设计模式——桥接模式
    设计模式——责任链模式
  • 原文地址:https://www.cnblogs.com/Fred1987/p/14874561.html
Copyright © 2011-2022 走看看