前端代码:

<Window x:Class="DataGridPractice.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DataGridPractice"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid Margin="0,0,0,214" Loaded="Grid_Loaded"> <Grid.Resources>
<DataTemplate x:Key="DataGridTemplateA">
<DataGrid Name="DataGrid2"
HeadersVisibility="None"
CanUserAddRows="False" CanUserDeleteRows="False"
CanUserResizeColumns="False" CanUserResizeRows="False"
CanUserReorderColumns="False" CanUserSortColumns="False"
AutoGenerateColumns="False" HorizontalAlignment="Left"
>
<DataGrid.Columns>
<DataGridTextColumn DisplayIndex="0" Binding="{Binding name, Mode=TwoWay}"/>
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</Grid.Resources> <DataGrid Name="DataGrid1"
CanUserAddRows="False" CanUserDeleteRows="False"
CanUserResizeColumns="False" CanUserResizeRows="False"
CanUserReorderColumns="False" CanUserSortColumns="False"
AutoGenerateColumns="False" HorizontalAlignment="Left"
VerticalContentAlignment="Center"> <DataGrid.Columns> <DataGridTextColumn DisplayIndex="0" Binding="{Binding id, Mode=TwoWay}" /> <DataGridTextColumn DisplayIndex="1" Binding="{Binding name, Mode=TwoWay}"/> <DataGridTextColumn DisplayIndex="2" Binding="{Binding age, Mode=TwoWay}"/> <DataGridTextColumn DisplayIndex="3" Binding="{Binding TotalCount, Mode=TwoWay}"/> <DataGridTemplateColumn DisplayIndex="4" CellTemplate="{StaticResource DataGridTemplateA}" IsReadOnly="True" Header="2"/> </DataGrid.Columns>
</DataGrid>
<TextBox HorizontalAlignment="Left" Height="38" Margin="117,310,0,-143" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="154" LostFocus="TextBox_LostFocus"/>
</Grid>
</Window>

后端代码:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
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.Threading; namespace DataGridPractice
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
LoadData();
}
public List<Person> list { get; set; } public List<Character> characterlist { get; set; }
public void LoadData()
{
Random r = new Random();
list = new List<Person>(); //Person person = new Person();
//person.id = 1;
//person.name = "张三1";
//person.age = r.Next(100);
//person.TotalCount = 5;
//list.Add(person); characterlist = new List<Character>(); Character chara1 = new Character();
chara1.name = "A";
characterlist.Add(chara1); Character chara2 = new Character();
chara2.name = "B";
characterlist.Add(chara2); Character chara3 = new Character();
chara3.name = "C";
characterlist.Add(chara3); for (int i = 0; i < 9; i++)
{ if (i > 1)
break; Person person = new Person();
person.id = i + 1;
person.name = "张三" + i;
person.age = r.Next(100);
person.TotalCount = 5; list.Add(person);
}
} private void Grid_Loaded(object sender, RoutedEventArgs e)
{
DataGrid1.ItemsSource = list; DataGridTemplateColumn templeColumn = DataGrid1.Columns[4] as DataGridTemplateColumn;
if (templeColumn == null) return; for (int i = 0; i < DataGrid1.Items.Count; i++)
{
DataGridCell dataGridCell = GetDataGridCell(i, 0);
if (dataGridCell != null)
{
DataGridCellInfo dataGridCellInfo = new DataGridCellInfo(dataGridCell); if (dataGridCellInfo.Item != null)
{
object item = dataGridCellInfo.Item; FrameworkElement element = templeColumn.GetCellContent(item); if (element != null)
{
DataGrid dataGrid2 = (DataGrid)templeColumn.CellTemplate.FindName("DataGrid2", element);
dataGrid2.ItemsSource = characterlist;
}
}
}
}
} public DataGridCell GetDataGridCell(int rowIndex, int columnIndex)
{
try
{
DataGridRow rowContainer = GetDataGridRow(rowIndex);
if (rowContainer != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
//这行代码是通过行得到单元格 DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
//这行代码是通过index得到具体的单元格 if (cell == null)
{
DataGrid1.ScrollIntoView(rowContainer, DataGrid1.Columns[columnIndex]);
cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
}
return cell;
}
}
catch
{
return null;
}
return new DataGridCell();
} public DataGridRow GetDataGridRow(int index)
{
if (index >= DataGrid1.Items.Count)
{
throw new IndexOutOfRangeException(String.Format("Index {0} is out of range.", index));
} DataGridRow row = (DataGridRow)DataGrid1.ItemContainerGenerator.ContainerFromIndex(index);
if (row == null)
{
// may be virtualized, bring into view and try again
DataGrid1.ScrollIntoView(DataGrid1.Items[index]);
WaitFor(TimeSpan.Zero, DispatcherPriority.SystemIdle);
row = (DataGridRow)DataGrid1.ItemContainerGenerator.ContainerFromIndex(index);
} return row;
}
public void WaitFor(TimeSpan time, DispatcherPriority priority)
{
DispatcherTimer timer = new DispatcherTimer(priority);
timer.Tick += new EventHandler(OnDispatched);
timer.Interval = time;
DispatcherFrame dispatcherFrame = new DispatcherFrame(false);
timer.Tag = dispatcherFrame;
timer.Start();
Dispatcher.PushFrame(dispatcherFrame);
}
public void OnDispatched(object sender, EventArgs args)
{
DispatcherTimer timer = (DispatcherTimer)sender;
timer.Tick -= new EventHandler(OnDispatched);
timer.Stop();
DispatcherFrame frame = (DispatcherFrame)timer.Tag;
frame.Continue = false;
} public T GetVisualChild<T>(Visual parent) where T : Visual
{
T childContent = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
childContent = v as T;
if (childContent == null)
{
childContent = GetVisualChild<T>(v);
}
if (childContent != null)
{
break;
}
}
return childContent;
} private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
DataGridTemplateColumn templeColumn = DataGrid1.Columns[4] as DataGridTemplateColumn;
if (templeColumn == null) return; for (int i = 0; i < DataGrid1.Items.Count; i++)
{
DataGridCell dataGridCell = GetDataGridCell(i, 0);
if (dataGridCell != null)
{
DataGridCellInfo dataGridCellInfo = new DataGridCellInfo(dataGridCell); if (dataGridCellInfo.Item != null)
{
object item = dataGridCellInfo.Item; FrameworkElement element = templeColumn.GetCellContent(item); if (element != null)
{
DataGrid dataGrid2 = (DataGrid)templeColumn.CellTemplate.FindName("DataGrid2", element);
dataGrid2.ItemsSource = characterlist; DataGridRow row = (DataGridRow)dataGrid2.ItemContainerGenerator.ContainerFromIndex(2);
row.Visibility = Visibility.Collapsed;
}
}
}
}
}
} public class Person
{
public int id { get; set; }
public string name { get; set; }
public int age { get; set; } public int TotalCount { get; set; }
} public class Character
{
public string name { get; set; }
}
}

效果图:

点击TextBox后再点击DataGrid效果图:

C# DataGrid嵌套DataGrid动态隐藏显示行的更多相关文章

  1. table 动态隐藏tr行

    table: <table style="width:100%" class="table01" cellspacing="1" ce ...

  2. Android 动态隐藏显示导航栏,状态栏

    Talk is cheap, show me the code. --Linus Torvalds Okay, here: 一.导航栏: [java] view plain copy private  ...

  3. 动态隐藏显示窗口的标题栏(同时保持窗口的sizeable性能

    今天考虑作界面的时候,想去掉窗体的标题栏,但设置 Form.BorderStyle := bsNone; 会导致窗体不可再 Sizeable (通过鼠标操作改变窗体大小),仔细翻看了相关Help也没找 ...

  4. easyui datagrid 表格动态隐藏部分列的展示

    1.一套代码中,可能不同的项目情况都在用,但是可能不同的项目要求展示的datagrid列的内容并不一致,所以能够动态的显示部分datagrid列的内容. 即datagrid的中的某一列,这个项目要求显 ...

  5. Easy DataGrid 实现动态列、行

    Easy DataGrid 实现动态列.行 前端代码: <title>展示销售的实时数据</title> <script type="text/javascri ...

  6. Easyui Datagrid 修改显示行号列宽度

    EasyUI中Datagrid的第一列显示行号,可是如果数据量大的的时候,显示行号的那一列数据会显示不完全的. 可以通过修改Datagrid的样式来解决这个问题,在样式中加入下面这个样式,就可以自己修 ...

  7. 重新=》easyui DataGrid是否可以动态的改变列显示的顺序

    $.extend($.fn.datagrid.methods,{ columnMoving: function(jq){ return jq.each(function(){ var target = ...

  8. 实现easyui datagrid在没有数据时显示相关提示内容

    本示例实现easyui datagrid加载/查询数据时,如果没有相关记录,则在datagrid中显示没有相关记录的提示信息,效果如下图所示 本实例要实现如下图所示的效果: 本示例easyui版本为1 ...

  9. easy ui datagrid在没有数据时显示相关提示内容

    $(function () { $('#dg').datagrid({ fitColumns: true, url: 'product.json', pagination: true, pageSiz ...

  10. 经历:easyui的datagrid没有数据滚动条的显示

    今天,一个用户提出一个这样的问题,"查询不到结果时,为什么我看不到后面的标题呢?" 最初,我听到这个问题时,第一反应是:查出来数据不就有滚动条了吗,干嘛非要较真呢? 不过,后来想想 ...

随机推荐

  1. vue-cli 更新遇到的问题,卸载不掉旧版本2.9.6(可行)

    今天更新vue-cli时,遇到问题:卸载不掉旧版本2.9 官网给的信息是:Vue CLI 的包名称由 vue-cli 改成了 @vue/cli. 如果你已经全局安装了旧版本的 vue-cli (1.x ...

  2. map方法整理数据,接口返回值进行处理

    整理前: //map方法使thumb加上域名 --> var data =[ { id: "11", title: "新车小程序title1", thum ...

  3. 基于Vue3的Leaflet基础

    1. 概述 Leaflet 是一个开源.轻量并且对移动端友好的交互式地图 JavaScript 库,大小仅仅只有 39 KB, 拥有绝大部分开发者所需要的所有地图特性 Leaflet 的官网为:Lea ...

  4. 【8】java之引用传递

    一.引用传递 ​引用传递是整个 java 的精髓所在 引用传递核心意义:同一块堆内存空间可以被不同的栈内存所指向,不同栈内存可以对同一块堆内存内容进行修改. 范例:第一道引用传递范例 class Me ...

  5. el-inpu 输入框,输入一个字符失去焦点,不能连续输入问题

    问题出现的原因:输入框绑定值改变导致代码从新渲染 <div v-for="(x,index) in item.newAttrs " :key="x.en" ...

  6. Echarts 圆形立体柱状图

    先放个效果图 const resData = [ { label: "上海", value: 66 }, { label: "北京", value: 26 }, ...

  7. asp.net.core学习笔记1:swagger的使用和webapi接收Jobject对象

    环境:asp.net.core 3.1 (一觉醒来官方已经不推荐3.0了,于是没有任何core经验,也只能开始了3.1的开发学习) 由于现有项目前后端分离.微服务化日趋流行,所以上手不采用web应用( ...

  8. kail 系统更新

    原文链接:https://blog.csdn.net/aiming66/article/details/123203495

  9. containerd 卸载

    一.创建脚本 cat > remove.sh <<EOF #!/bin/bash # 删除contained命令及配置 rm -rf /usr/local/bin/ rm -rf / ...

  10. 泛微e-cology OA 远程代码执行漏洞复现

    复现过程: 直接使用FOFA搜索语法搜索,找了好久才找到含有漏洞的站点. 在根目录后面输入漏洞路径: /weaver/bsh.servlet.BshServlet 进入BeanShell操作页面 在S ...