首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > CAD教程 >

silverght 4 中 DataGrid 中 全选/单选解决方案

2012-03-22 
silverght 4中 DataGrid中全选/单选在silverght4.中,如何设置,并且,取得,所选择的ID。[解决办法]http://www

silverght 4 中 DataGrid 中 全选/单选


在silverght 4.中,如何设置,并且,取得,所选择的ID。

[解决办法]
http://www.cnblogs.com/dino623/archive/2012/01/22/2328874.html
[解决办法]
using System.Linq;
using System.Windows;
using System.Windows.Controls;

using Expression = System.Linq.Expressions.Expression;
using System;

namespace CustomCheckBoxColumn_SL
{
public class CustomCheckBoxColumn : GridViewDataColumn
{
public override FrameworkElement CreateCellElement(GridView.GridViewCell cell, object dataItem)
{
if (cell != null)
{
CheckBox checkBox = cell.Content as CheckBox;
if (checkBox == null)
{
checkBox = new CheckBox();
checkBox.SetBinding(CheckBox.IsCheckedProperty, this.DataMemberBinding);

if (cell.ParentOfType<DataControl>() != null)
{
StyleManager.SetTheme(checkBox, StyleManager.GetTheme(cell.ParentOfType<DataControl>()));
}
}
return checkBox;
}

return base.CreateCellElement(cell, dataItem);
}

private CheckBox checkBoxInHeader = null;

public override object Header
{
get
{
if (this.checkBoxInHeader == null && this.DataControl != null
&& this.DataControl.SelectionMode != System.Windows.Controls.SelectionMode.Single)
{
var headerCheckBox = new CheckBox();
headerCheckBox.Click += new RoutedEventHandler(headerCheckBox_Click);

this.checkBoxInHeader = headerCheckBox;
}

return this.checkBoxInHeader;
}
set
{
this.Header = value;
}
}

void headerCheckBox_Click(object sender, RoutedEventArgs e)
{
var headerCheckBox = (CheckBox) sender;

if (this.DataControl == null)
return;

this.SelectOrDeselectAllItems(headerCheckBox);
}


private void SetPropertyValue(object item, bool value) 
{
//First option - using Reflection
var property = item.GetType().GetProperties().Where((p) => p.Name == this.DataMemberBinding.Path.Path).FirstOrDefault();
property.SetValue(item, value, null);


//Second option - using Expressions
//var propertyExpression = Expression.Property(Expression.Constant(item), this.DataMemberBinding.Path.Path);
//var propertyAssign = Expression.Assign(propertyExpression, Expression.Constant(value));
//var funk = Expression.Lambda<Func<bool>>(propertyAssign).Compile();

//funk.Invoke();
}


private void SelectOrDeselectAllItems(CheckBox headerCheckBox)
{
if (headerCheckBox.IsChecked == true)
{

foreach (Player item in this.DataControl.Items)
{
this.SetPropertyValue(item, true);
//this.DataMemberBinding.Path.Path = true;
//prpertyInfo.SetValue(item, false);
}
}
else if (headerCheckBox.IsChecked == false)
{
foreach (Player item in this.DataControl.Items)
{
this.SetPropertyValue(item, false);
}

}
}

public CheckBox GetCheckBoxInHeader()
{
return this.Header as CheckBox;
}
}
}

热点排行