原文:WPF 实现主从的datagrid以及操作rowdetailtemplate 的方法

WPF 实现主从的datagrid以及操作rowdetailtemplate 的方法

       最近在做一个项目,其中要用到主从表数据的折叠和隐藏,并且对从表中的数据能够获取和操作,虽然搞了好久,但最终被我推敲出来了,写出来分享一下,让朋友少走弯路,废话不说了,先上效果图:

下面是代码:

 
前台   
<DataGrid ItemsSource="{Binding}"Name="dataGrid1"
MouseUp="dataGrid1_MouseUp"...>   
<DataGrid.Columns>   
                   <DataGridTemplateColumn Width="Auto">   
                       <DataGridTemplateColumn.CellTemplate>   
                           <DataTemplate>   
                               <Expander Expanded="Expander_Expanded"Collapsed="Expander_Collapsed"/>   
                           </DataTemplate>   
                       </DataGridTemplateColumn.CellTemplate>   
                   </DataGridTemplateColumn>   
                   <DataGridTextColumn Binding="{Binding Facility_type}"FontSize="22"
Header="設備類型"  
Width=
"120"/>   
                   <DataGridTextColumn  FontSize="22"Header="廠房代碼"
Width="85"
/>   
                   <DataGridTextColumn  FontSize="22" Header="樓層"
Width="70"
/>   
                   <DataGridTextColumn  FontSize="22"  Header="部門名稱"
Width="430"
/>   
                   <DataGridTextColumn Binding="{Binding Count_all}"FontSize="22"   
Header="設備總數" 
Width=
"100"/>   
                   <DataGridTextColumn Binding="{Binding Count_no}"FontSize="25" 
FontWeight="Bold"  
Foreground=
"Red" Header="未使用數量" Width="115"
/>   
                   <DataGridTextColumn Binding="{Binding Count_yes}"FontSize="22" 
Foreground="Green" 
Header=
"使用中數量"  Width="115"/>   
         
               
</DataGrid.Columns>   
               
<DataGrid.RowDetailsTemplate >   
                   <DataTemplate  >   
         
                       <DataGrid Name="dataGrid2" Width="1070" 
SelectedValuePath = "dept_code"
IsReadOnly="True" 
HeadersVisibility=
"None"HorizontalGridLinesBrush="#FFE0E2DF"VerticalGridLinesBrush="#FFE0E2DF"
                                 ItemsSource="{Binding Details}"CanUserAddRows="False"SelectionUnit="FullRow" AutoGenerateColumns="False" MouseUp="dataGrid2_MouseUp">   
                           <DataGrid.Columns>   
                               <DataGridTextColumn   Header=""  Width="150"
/>   
                               <DataGridTextColumn Binding="{Binding building_code}"FontSize="22"
Header="廠房代碼"
Width=
"85"/>   
                               <DataGridTextColumn Binding="{Binding floor}"FontSize="22" 
Header="樓層"
Width=
"70"/>   
                               <DataGridTextColumn Binding="{Binding dept_name}"FontSize="22"  
Header="部門名稱"
Width=
"430"/>   
                               <DataGridTextColumn Binding="{Binding count_all}"FontSize="22"   
Header="設備總數" 
Width=
"100"/>   
                               <DataGridTextColumn Binding="{Binding count_no}" FontSize="25" 
FontWeight="Bold" 
Foreground=
"Red" Header="未使用數量" 
Width="115"
/>   
                               <DataGridTextColumn Binding="{Binding count_yes}"FontSize="22" 
Foreground="Green" 
Header=
"使用中數量"  Width="115"/>   
                           </DataGrid.Columns>   
                    </DataTemplate>   
               
</DataGrid.RowDetailsTemplate>   
...   
</DataGrid>

下面是后台:

 
using System;    
using System.Data;    
using System.Windows;    
using System.Windows.Input;    
using System.Windows.Media;    
using System.Windows.Controls;    
using System.Collections.ObjectModel;    
using System.Windows.Threading;    
namespace
Getac.DSP    
{    
    /// <summary>    
    /// HumanWork.xaml 的交互逻辑    
    /// </summary>    
    publicpartial
class FacilitySum:Window    
    {    
        public string Company_code;    
       // public string Select_item;    
        publicObservableCollection<Facility> Items {
get;set; }    
        privatedelegate
void ThreadDelegate();
//申明一个专用来调用更改线程函数的委托    
     publicDispatcherTimer ShowTimer;    
        publicFacilitySum(string company_code)    
        {    
           
this.Company_code = company_code;    
           
InitializeComponent();    
           
this.WindowState = WindowState.Maximized;    
         
}    
           
        privatevoid
Window_Loaded(object sender, RoutedEventArgs e)    
        {     
          
//datagrid数据的加载    
           
Items = new
ObservableCollection<Facility>();    
           
DataSet dsFacility = newBLL.DSP_FACILITY_USE().GetFacility_status_Sum(Company_code);    
           
DataSet dsFacilityDetail = newBLL.DSP_FACILITY_USE().GetFacility_UseStatus(Company_code);    
           
int count = dsFacility.Tables[0].Rows.Count;    
           
for (inti =
0; i < count; i++)    
           
{    
               
Items.Add(new
Facility(Company_code, i, dsFacility, dsFacilityDetail));    
           
}    
           
dataGrid1.Items.Clear();    
           
dataGrid1.ItemsSource  = Items;    
           
dataGrid1.Items.Refresh();    
           
dataGrid1.SelectedValuePath = "facility_type";    
          
}    
           
        //展开,收缩子表的方法     
        privatevoid
Expander_Expanded(object sender, RoutedEventArgs e)    
        {    
           
DataGridRow row = FindVisualParent<DataGridRow>(sender
as Expander);    
           
row.DetailsVisibility = System.Windows.Visibility.Visible;    
        }    
                    
        privatevoid
Expander_Collapsed(object sender, RoutedEventArgs e)    
        {    
           
DataGridRow row = FindVisualParent<DataGridRow>(sender
as Expander);    
           
row.DetailsVisibility = System.Windows.Visibility.Collapsed;    
        }    
        publicT FindVisualParent<T>(DependencyObject child) where T : DependencyObject    
        {    
           
DependencyObject parentObject = VisualTreeHelper.GetParent(child);    
           
if (parentObject ==
null) returnnull;    
           
T parent = parentObject asT;    
           
if (parent != null)    
               
return parent;    
           
else
               
return FindVisualParent<T>(parentObject);    
        }    
        
//下面的方法曾让我教头烂额,感叹微软的控件封装的太牛逼了,处理起来有点变态    
        /// <summary>    
        /// 找到行明细中嵌套的控件名称    
        /// </summary>    
        /// <typeparam name="T"></typeparam>    
        /// <param name="parent"></param>    
        /// <param name="name"></param>    
        /// <returns></returns>    
        publicT FindVisualChildByName<T>(DependencyObject parent, string name) where T : DependencyObject    
        
{    
            
if(parent !=null)    
            
{    
                for(inti=0;i<VisualTreeHelper.GetChildrenCount(parent);i++)    
                {    
                    var
child = VisualTreeHelper.GetChild(parent, i)as
DependencyObject;    
                    string controlName=child.GetValue(Control.NameProperty)as
string;    
                    if(controlName==name)    
                    {    
                        return
child as
T;    
                    }    
                    else
                    {    
                        T result=FindVisualChildByName<T>(child,name);    
                        if(result!=null)    
                            return
result;    
                    }    
                }    
            
}    
            
return null;    
        
}    
           
        
//父表的事件处理驱动    
        privatevoid
dataGrid1_MouseUp(object sender, MouseButtonEventArgs e)    
       
{    
           
if (dataGrid1.CurrentCell.Column !=null
&& dataGrid1.CurrentCell.Column.Header!=null)    
           
{    
               string facility_type = (dataGrid1.Columns[1].GetCellContent(dataGrid1.CurrentCell.Item)as
TextBlock).Text;    
               string head = dataGrid1.CurrentCell.Column.Header.ToString();    
                 //这边可以根据实际写自己的一些方法    
            
}    
       
}    
           
      //字表,也就是rowdetailtemplate中的datagrid 的事件处理方法,其中有调用上面的约束泛型类型的方法。    
       
private void
dataGrid2_MouseUp(object sender, MouseButtonEventArgs e)    
       
{    
            
//下面的两行代码很关键    
       
DataGridRow dgr = (DataGridRow)dataGrid1.ItemContainerGenerator.ContainerFromIndex(this.dataGrid1.SelectedIndex);    
           
DataGrid dg= FindVisualChildByName<DataGrid>(dgr,
"dataGrid2") asDataGrid;    
            
//获取鼠标点击的行每一单元格的值   
           
string facility_type = (dataGrid1.Columns[1].GetCellContent(dataGrid1.CurrentCell.Item)as
TextBlock).Text;    
           
string building_code = (dg.Columns[1].GetCellContent(dg.CurrentCell.Item)as
TextBlock).Text;    
           
string floor = (dg.Columns[2].GetCellContent(dg.CurrentCell.Item)as
TextBlock).Text;    
           
string dept_code =dg.SelectedValue.ToString();    
           
string head = dg.CurrentCell.Column.Header.ToString();    
           
//获得一些信息后你可以自己写方法实现需要的功能    
       
}    
    }    
    // 定义集合类用于存放栏位值    
    publicclass
Facility    
    {    
       
//static string FACILITY_TYPE;    
       
public string Facility_type{get;
set; }    
       
public string Building_code {get;
set; }    
       
public string Floor {
get; set; }    
       
public string Dept_code {get;
set; }    
       
public string Count_all {get;
set; }    
       
public string Count_no {get;
set; }    
       
public string Count_yes {get;
set; }    
       
public ObservableCollection<Facility_Detail> Details {get;
set; }    
       
public Facility(string company_code,int
row_index,DataSet dsfacilitySum ,DataSet dsFacilityDetail)    
       
{    
           
           
Facility_type = dsfacilitySum.Tables[0].Rows[row_index]["facility_type"].ToString();    
           
Count_all = dsfacilitySum.Tables[0].Rows[row_index]["count_all"].ToString();    
           
Count_no = dsfacilitySum.Tables[0].Rows[row_index]["count_no"].ToString();    
           
Count_yes = dsfacilitySum.Tables[0].Rows[row_index]["count_yes"].ToString();    
           
Details = new
ObservableCollection<Facility_Detail>();    
           
//详细信息    
       
System.Data.DataTable dtDetail = newSystem.Data.DataTable();    
           
DataRow[] rowDetail = dsFacilityDetail.Tables[0].Select("facility_type='"+ Facility_type +
"'");    
           
dtDetail = dsFacilityDetail.Tables[0].Clone();    
            
foreach (DataRow dr inrowDetail)    
            
{    
                Details.Add(newFacility_Detail()    
                {    
                    facility_type = Facility_type,    
                    building_code = dr["building_code"].ToString(),    
                    floor = dr["floor"].ToString(),    
                    dept_code = dr["dept_code"].ToString(),    
                    dept_name = dr["dept_name"].ToString(),    
                    count_all = dr["count_all"].ToString(),    
                    count_no = dr["count_no"].ToString(),    
                    count_yes = dr["count_yes"].ToString()    
                });    
            
}    
       
}    
    }    
    publicclass
Facility_Detail    
    {  //定义属性 
       
public string facility_type {get;
set; }    
       
public string building_code {get;
set; }    
       
public string floor {
get; set; }    
       
public string dept_code {get;
set; }    
       
public string dept_name {get;
set; }    
       
public string count_all {get;
set; }    
       
public string count_no {get;
set; }    
       
public string count_yes {get;
set; }    
    }    
}

代码虽然很多,但是看下来不觉得繁琐。在这里还要感叹一下,微软的东西,尤其是控件这种东西封装的太死了,你用第三方的控件其实还不如自己写,能明白很多东西。欢迎大家批评指正。

WPF 实现主从的datagrid以及操作rowdetailtemplate 的方法的更多相关文章

  1. easyui datagrid 动态操作editor 的方法

    easyui本身是不提供这么细节的功能的,需要我们自己拓展下: 在easyui.min.js中扩展: $.extend($.fn.datagrid.methods, { addEditor : fun ...

  2. WPF+MVVM学习总结 DataGrid简单案例

    一.WPF概要 WPF(Windows Presentation Foundation)是微软推出的基于Windows 的用户界面框架,属于.NET Framework 3.0的一部分.它提供了统一的 ...

  3. MSDN 杂志:UI 前沿技术 - WPF 中的多点触控操作事件

    原文  MSDN 杂志:UI 前沿技术 - WPF 中的多点触控操作事件 UI 前沿技术 WPF 中的多点触控操作事件 Charles Petzold 下载代码示例 就在过去几年,多点触控还只是科幻电 ...

  4. WPF DataGrid 列宽填充表格方法

    WPF中使DataGrid 列宽填充表格方法,设置ColumnWidth属性为ColumnWidth="*"即可. 源码: <DataGrid AutoGenerateCol ...

  5. Asp.Net MVC +EntityFramework主从表新增编辑操作的实现(删除操作怎么实现?)

    Asp.Net MVC +EntityFramework主从表新增编辑操作的实现 对于MVC中同时对主从表的表单操作在网上现有的解决很少,而这样的操作在做业务系统中是经常为遇到的.我在网上搜索了很久都 ...

  6. WPF Window背景半透明 ,蒙版操作实现

    原文:WPF Window背景半透明 ,蒙版操作实现 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/BYH371256/article/detail ...

  7. WPF自定义样式篇-DataGrid

    WPF自定义样式篇-DataGrid 先上效果图: 样式:  <!--DataGrid样式-->    <Style TargetType="DataGrid"& ...

  8. MVVM框架从WPF移植到UWP遇到的问题和解决方法

    MVVM框架从WPF移植到UWP遇到的问题和解决方法 0x00 起因 这几天开始学习UWP了,之前有WPF经验,所以总体感觉还可以,看了一些基础概念和主题,写了几个测试程序,突然想起来了前一段时间在W ...

  9. 一些ES5的操作数组的方法

    在ES5规范中新增了不少操作数组的方法,特此罗列一下以备使用 1. forEach循环 有点类似jQuery的each循环 [12,23,36,4,5].forEach(function(v,k){ ...

随机推荐

  1. openvswitch技术总结

    OVS技术总结 一.OVS的组成 二.OVS的匹配条件和动作 三.OVS的发展方向 四.OVS的操作实践 OVS与Namespace配合模拟租户之间的数据通信 基本思路: Namespace模拟出不同 ...

  2. Android系统启动流程 总结

    整体流程大致如下:   Android系统的启动,主要是指Android手机关机后,长按电源键后,Android手机开机的过程.从系统角度看,Android的启动程序可分为:   1.bootload ...

  3. 使用过AsyncTask、EventBus、Volley以及Retrofit,必须好好了解handler运行机制

    我们都知道在UI线程中不能进行耗时操作,例如数据读写.网络请求.Android 4.0开始,在主线程中进行网络请求甚至会抛出Android.os.NetworkOnMainThreadExceptio ...

  4. Mac 的mysql5.7没有配置文件,如何解决only_full_group_by 问题

    数据库版本是5.7.19,在写语句的时候,只要涉及ORDER BY,就会报错, ERROR 1055 (42000): Expression #7 of SELECT list is not in G ...

  5. (网页)Http状态码大全(200、404、500等)(转CSDN)

    当我们从客户端向服务器发送请求时 服务器向我们返回状态码 状态码就是告诉我们服务器响应的状态 通过它,我们就可以知道当前请求是成功了还是出现了什么问题 状态码是由3位数字和原因短语组成的(比如最常见的 ...

  6. Sql 查询结果 根据某个字段值 变更另外一个字段值 case when

    SELECT CASE THEN '*******' ELSE Plate END AS Plate, CarType FROM Cars;

  7. PL/SQL 删除主键 ORA-02443: 无法删除约束条件-不存在的约束条件

    在PL/SQL developer中删除一个表的主键,然后把另外一个字段设置成主键,删除的过程中报错:ORA-02443 我遇到这个问题出现的背景是: alter table saleqtya dro ...

  8. zookeeper-01 概述

    1. Zookeeper概念简介和应用场景 1.1.  概念简介 Zookeeper是一个分布式协调服务:就是为用户的分布式应用程序提供协调服务 A.zookeeper是为别的分布式程序服务的 B.Z ...

  9. gcc库链接

    转载于https://blog.csdn.net/zhangdaisylove/article/details/45721667 1.库的分类 库有静态库和动态库,linux下静态库为.a,动态库为. ...

  10. 17秋 软件工程 团队第五次作业 Alpha Scrum2

    17秋 软件工程 团队第五次作业 Alpha Scrum2 今日完成的任务 杰麟:Java后端的学习: 世强:登录和注册接口编写: 港晨:完成数据库表的设计: 树民.陈翔:完成超级管理员后端框架. 其 ...