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

2013/04/02 · by paul van bladel · in Uncategorized

Introduction

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

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)

lightswitch Grid 控件添加 CheckBox 多选的更多相关文章

  1. Android控件之CheckBox(复选框控件)

    一.有两种状态: 选中状态(true).未选中状态(false) 二.属性 android:id = "@+id/checkbox" android:layout_width=&q ...

  2. Grid控件

    Grid控件是WPF布局容器中功能最强大.最灵活的控件.Grid控件基本上能够完成其他WPF容器控件所能完成的功能,Microsoft建议大多数界面的布局都使用Grid控件来实现,因此默认情况下.vs ...

  3. FineUI Grid控件右键菜单的实现

    FineUI官方Demo上一直没有Grid右键菜单的实现,其实从4.1.x的版本开始,允许添加自定义的事件监听(Listeners),所以要实现这个功能已经相当容易了. ExtJs右键菜单有很多种,对 ...

  4. 通过编写串口助手工具学习MFC过程——(五)添加CheckBox复选框

    通过编写串口助手工具学习MFC过程 因为以前也做过几次MFC的编程,每次都是项目完成时,MFC基本操作清楚了,但是过好长时间不再接触MFC的项目,再次做MFC的项目时,又要从头开始熟悉.这次通过做一个 ...

  5. FineUI Grid控件高度自适应

    引言 页面里使用f:Grid控件,添加分页功能,然后高度填充整个页面. 如何使用 使用FineUI 控件的每个页面都有一个f:PageManager控件,它包含属性:AutoSizePanelID,设 ...

  6. WPF平台Grid控件性能比较

    WPF官方发布第一个版本至今已经有10年了, 我们几乎在同时也开始了XAML开发.即使经过多年打造,我们依旧尝试提高:我们真的成功打造了高效灵活的控件吗?我没有在其他地方找到任何关于优秀的WPF表格性 ...

  7. asp.net中的ListBox控件添加双击事件

    问题:在Aspx页里的ListBox A中添加双击事件,将选中项添加到另一个ListBox B中,双击ListBox B中的选中项,删除当前选中项 页面: <asp:ListBox ID=&qu ...

  8. 实现控件WPF(4)----Grid控件实现六方格

    PS:今天上午,非常郁闷,有很多简单基础的问题搞得我有些迷茫,哎,代码几天不写就忘.目前又不当COO,还是得用心记代码哦! 利用Grid控件能很轻松帮助我们实现各种布局.上面就是一个通过Grid单元格 ...

  9. 在C#中使用属性控件添加属性窗口

    转自原文 在C#中使用属性控件添加属性窗口 第一步,创建在应用程序中将要展现的字段属性为public公有属性.其中,所有的属性必须有get和set的方法(如果不设置get方法,则要显示的属性不会显示在 ...

随机推荐

  1. tween 缓动动画

    在讲tween类之前,不得不提的是贝塞尔曲线了.首先,贝塞尔曲线是指依据四个位置任意的点坐标绘制出的一条光滑曲线.它在作图工具或动画中中运用得比较多,例如PS中的钢笔工具,firework中的画笔等等 ...

  2. js原生的节点操作API

    // yi获取元素节点 //一 :过id的方式( 通过id查找元素,大小写敏感,如果有多个id只找到第一个) document.getElementById('div1'); // 通过类名查找元素, ...

  3. linux虚拟机配置上网(静态IP)和配置tomcat服务环境

    常用命令:vi或者vim编辑 ,按i编辑模式,按ecs进入基本模式,按 :w  保存:按 :wq  退出并保存:mv移动::q退出 :ln -sv apache-tomcat-8.0.24 tomca ...

  4. solr7.7.0搜索引擎使用(四)(搜索语法)

    solr搜索语法 参数defType   指定用于处理查询语句(参数q的内容)的查询解析器,eg:defType=lucenesort    指定响应的排序方式:升序asc或降序desc.同时需要指定 ...

  5. shell脚本监控系统负载、CPU和内存使用情况

    hostname >>/home/vmuser/xunjian/xj.logdf -lh >>/home/vmuser/xunjian/xj.logtop -b -n 1 | ...

  6. 参考文献bib管理

    比如在IEEE模板中,在当前目录添加 bib 文件reference.bib 在 \end{document} 之前加入 \bibliographystyle{IEEEtran} \bibliogra ...

  7. Makefile入门

    相信大家对makefile都不陌生,在Linux下编写程序基本都离不开makefile的编写,我们都知道多个.c文件经过编译器编译后得到多个.o文件,这些文件是互相独立的,但最终我们要得到一个可正常运 ...

  8. 一个友盟BUG的思考和分析:Invalid update

    1.友盟错误信息 Invalid update: invalid number of rows . The number of rows contained ) must be equal to th ...

  9. 【算法】二叉查找树(BST)实现字典API

    参考资料 <算法(java)>                           — — Robert Sedgewick, Kevin Wayne <数据结构>       ...

  10. ZZNU 2098 Drink coffee(差分+树状数组)

    题目链接:http://acm.hi-54.com/problem.php?pid=2098 2098 : Drink coffee 时间限制:1 Sec 内存限制:256 MiB 提交:32 答案正 ...