因为WPF中的DataGrid达不到开发中的要求,所以就试着改了一下。改后感觉效果不错,整理后发出来,希望大家多多批评指教。
主要修改如下。
1。增加合计单元格功能。
2。每行有一个CheckBox,表示选择该行。
PopupInfo.xaml(用作显示合计数)
<sc:Popup x:Class="Controls.PopupInfo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
xmlns:sc="clr-namespace:System.Windows.Controls.Primitives;assembly=PresentationFramework" >
<sc:Popup.Child>
<StackPanel >
<Border BorderBrush="Silver" BorderThickness="1" Background="#FF47BBF2">
<TextBlock x:Name="TotalLB" Margin="6,3" ></TextBlock>
</Border>
</StackPanel>
</sc:Popup.Child>
</sc:Popup>
PopupInfo.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
namespace Controls
{
/// <summary>
/// PopupInfo.xaml 的交互逻辑
/// </summary>
public partial class PopupInfo : Popup
{
public PopupInfo()
{
InitializeComponent();
}
public void Display(string mess)
{
this.TotalLB.Text = mess;
this.IsOpen = true;
}
public void Hide()
{
this.IsOpen = false;
}
}
}
DataGridEx.xaml
<smc:DataGrid x:Class="Controls.DataGridEx"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
xmlns:smc="clr-namespace:System.Windows.Controls;assembly=PresentationFramework"
xmlns:my="clr-namespace:Controls"
CanUserAddRows="False" CanUserDeleteRows="False"
SelectionUnit="CellOrRowHeader"
MouseUp="DataGridEx_MouseUp"
MouseDoubleClick="DataGrid_MouseDoubleClick">
<smc:DataGrid.Resources>
<my:PopupInfo x:Key="popupInfoKey" Placement="Mouse"></my:PopupInfo>
</smc:DataGrid.Resources>
</smc:DataGrid>
DataGridEx.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Controls
{
/// <summary>
/// DataGridEx.xaml 的交互逻辑
/// </summary>
public partial class DataGridEx : DataGrid
{
public DataGridEx()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(DataGridEx_Loaded);
}
private PopupInfo pInfo = null;
void DataGridEx_Loaded(object sender, RoutedEventArgs e)
{
pInfo = this.FindResource("popupInfoKey") as PopupInfo;
DataGridCheckBoxColumn seleCol = new DataGridCheckBoxColumn();
seleCol.Header = "选择";
this.Columns.Insert(0, seleCol);
this.Loaded -= DataGridEx_Loaded;
}
//显示合计
private void DataGridEx_MouseUp(object sender, MouseButtonEventArgs e)
{
if (this.SelectedCells.Count > 1)
{
try
{
decimal total = 0;
foreach (DataGridCellInfo cell in this.SelectedCells)
{
TextBlock cellTB = cell.Column.GetCellContent(cell.Item) as TextBlock;
total += Convert.ToDecimal(cellTB.Text);
}
this.pInfo.Display("合计:"+total);
return;
}
catch
{
}
}
pInfo.Hide();
}
public bool DoubleClickCheckItem { get; set; }
//双击选中CheckBox
private void DataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if ( this.CurrentCell != null)
{
this.SelectedItem = this.CurrentCell.Item;
if (DoubleClickCheckItem)
{
CheckBox cc = (this.Columns[0].GetCellContent(this.CurrentCell.Item)) as CheckBox;
if (cc != null)
{
cc.IsChecked = !cc.IsChecked;
}
}
}
}
//返回选中项
public List<object> CheckedItems()
{
List<object> result = new List<object>();
foreach (object item in this.Items)
{
CheckBox cc = (this.Columns[0].GetCellContent(item)) as CheckBox;
if (cc.IsChecked == true)
{
result.Add(item);
}
}
return result;
}
//全选
public void CheckAll()
{
foreach (object item in this.Items)
{
CheckBox cc = (this.Columns[0].GetCellContent(item)) as CheckBox;
cc.IsChecked = true;
}
}
//取消全选
public void ClearAllChecked()
{
foreach (object item in this.Items)
{
CheckBox cc = (this.Columns[0].GetCellContent(item)) as CheckBox;
cc.IsChecked = false;
}
}
}
}
调用方法与一般的DataGrid一样。调用CheckAll全选,调用ClearAllChecked取消全选,CheckedItems返回选中项。DoubleClickCheckItem=true时双击单元格时选中该记录。
