zoukankan      html  css  js  c++  java
  • lightswitch Grid 控件添加 CheckBox 多选

    ACTIVATING MULTI SELECTION WITH CHECKBOXES IN A LIGHTSWITCH GRID WITH A ONE-LINER

    Introduction

    In a business app grid, you often need the ability the select multiple rows and apply an action on the selected rows.

    multiselectgrid

    Wouldn’t it be great if we could inject this via following “one-liner”;

    ObservableCollection<Product> SelectedProducts = new ObservableCollection<Product>();
     
           partial void EditableProductsGrid1_InitializeDataWorkspace(List<IDataService> saveChangesTo)
           {
               this.FindControl("grid").AddCheckBoxColumnForMultiSelection<Product>(SelectedProducts);
           }

    The line that really matters is the extension method “AddCheckBoxColumnForMultiSelection”. It takes at input an ObservableCollection which will hold the selected items.

    As you can see, there is a button called “Do Something With Selection”. We can tweak also the CanExecute behavior as follows:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    partial void DoSomethingWithSelection_Execute()
            {
                foreach (var item in SelectedProducts)
                {
                    this.ShowMessageBox(item.ProductName);
                }
            }
     
    partial void DoSomethingWithSelection_CanExecute(ref bool result)
            {
                result = this.Products.SelectedItem != null && SelectedProducts.Count >= 1; ;
            }

    This will make sure the button is not clickable if there are no checkboxes checked.

    What’s our base infrastructure for doing this?

    That’s a bit more involved. But it doesn’t matter, since it’s implemented as an extension method, the whole functionality becomes a … one liner.

    using Microsoft.LightSwitch;
    using Microsoft.LightSwitch.Client;
    using Microsoft.LightSwitch.Presentation;
    using System;
    using System.Linq;
    using System.Collections.ObjectModel;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Markup;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using Microsoft.LightSwitch.Framework.Client;
    using Microsoft.LightSwitch.Presentation.Extensions;
    using System.Diagnostics;
    using Microsoft.LightSwitch.Presentation.Implementation;
    using System.Collections.Generic;
    namespace LightSwitchApplication
    {
        public static class MultiSelectGridExtensions
        {
            public static void AddCheckBoxColumnForMultiSelection<T>(this IContentItemProxy gridProxy, ObservableCollection<T> selectedItems) where T : class,IEntityObject
            {
                EventHandler<ControlAvailableEventArgs> gridProxy_ControlAvailable = null;
     
                gridProxy_ControlAvailable = (s1, e1) =>
                {
                    DataGrid dataGrid = e1.Control as DataGrid;
                    var contentItem = dataGrid.DataContext as IContentItem;
                    var visualCollection = (contentItem.Value) as IVisualCollection;
                    dataGrid.LoadingRow += new EventHandler<DataGridRowEventArgs>((s2, e2) =>
                    {
                        DataGridColumn column = dataGrid.Columns[0];
                        var checkBox = column.GetCellContent(e2.Row) as CheckBox;
                        T currentRowItem = e2.Row.DataContext as T;
                        if (currentRowItem != null)
                        {
                            checkBox.IsChecked = selectedItems.Contains(currentRowItem);
                        }
     
                        RoutedEventHandler checkboxClick = null;
                        checkboxClick = (
                            (s3, e3) =>
                            {
                                var selectedItem = dataGrid.SelectedItem as T;
                                if (selectedItem == null)
                                {
                                    return;
                                }
                                if (checkBox.IsChecked ?? false)
                                {
                                    if (!selectedItems.Contains(selectedItem))
                                    {
                                        selectedItems.Add(selectedItem);
                                    }
                                }
                                else
                                {
                                    selectedItems.Remove(selectedItem);
                                }
                                TriggerCanExecute(visualCollection);
                            });
                        checkBox.Click += checkboxClick;
                    });
     
                    var col = new DataGridTemplateColumn();
                    var xaml =
                        @"<DataTemplate  xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">
                        <CheckBox/>
                    </DataTemplate>";
                    var dataTemplate = XamlReader.Load(xaml) as DataTemplate;
                    col.CellTemplate = dataTemplate;
                    dataGrid.Columns.Insert(0, col);
     
                    gridProxy.ControlAvailable -= gridProxy_ControlAvailable;
                };
     
                gridProxy.ControlAvailable += gridProxy_ControlAvailable;
            }
            private static void TriggerCanExecute(IVisualCollection visualCollection)
            {
                //this will make sure that the CanExecute method is triggered in a potential button for doing something
                //with the selection result.
                //not elegant, but no other option...
                var currentItem = visualCollection.SelectedItem;
                var collection = visualCollection as IEnumerable<IEntityObject>;
     
                if (!visualCollection.SelectedItem.Equals(collection.Last()))
                {
                    visualCollection.SelectedItem = collection.Last();
                }
                else
                {
                    visualCollection.SelectedItem = collection.First();
                }
                visualCollection.SelectedItem = currentItem;
            }
        }
     
    }

    Can I download it?

    No, you can’t because I stopped uploading samples, as from today. Full stop.

    But, … you can directly inject the functionality with NuGet into the silverlight client project. (at least when you use the latest LightSwitch version)

  • 相关阅读:
    Qt——组件位置随窗口变化
    (转) Qt 出现“undefined reference to `vtable for”原因总结
    在CentOS上安装和部署Shiny Server
    Hibernate实体生成JSON的问题及解决
    在CentOS上安装并运行SparkR
    CentOS 6主机上的RStudio Server安装步骤
    Oracle用户密码过期后重置SYS用户密码
    在CentOS中将/var等已有目录挂载到新添加的硬盘
    CentOS中的常用命令
    Java在Web项目中读取properties文件
  • 原文地址:https://www.cnblogs.com/neozhu/p/3680636.html
Copyright © 2011-2022 走看看