先上图:

要求:对第一行的“选项内容举例。。。”的控件进行隐藏,如下:

前端代码:

<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" Loaded="Window_Loaded">
<Grid>
<Grid.Resources> <DataTemplate x:Key="DateTemplate" >
<StackPanel DataContext="{Binding choseItems}">
<Border Background="LightBlue">
<TextBox Text="{Binding ChoseName}" Cursor="Arrow" Name="R0"/>
</Border>
<Border Background="White">
<TextBox Text="{Binding ChoseContent}" Cursor="Arrow" Name="R1"/>
</Border>
</StackPanel>
</DataTemplate> <DataTemplate x:Key="EditingDateTemplate"> </DataTemplate>
</Grid.Resources> <DataGrid AutoGenerateColumns="False" Name="datagrid1">
<DataGrid.Columns>
<DataGridTextColumn Header="ID值" Width="auto" Binding="{Binding questionID}" />
<DataGridTextColumn Header="题目" Width="auto" Binding="{Binding questionName}" />
<DataGridTemplateColumn Header="多行" Width="*" MinWidth="25"
CellTemplate="{StaticResource DateTemplate}" CellEditingTemplate="{StaticResource EditingDateTemplate}" IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>

后端代码;

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
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();
} class question
{
//题目ID号
public Int32 questionID { get; set; } //题目名,比如:第1题或第2题等
public string questionName { get; set; } //某题中"选项类"的集合,比如A-D
public ObservableCollection<choseItem> choseItems { get; set; }
public question(Int32 _id, string _questionname, ObservableCollection <choseItem> _choseitems)//构造函数
{
questionID = _id;
questionName = _questionname;
choseItems = _choseitems;
}
}
//选项类
class choseItem
{
//选项名,比如:A,B,C,D之类
public string ChoseName { get; set; }
//选项内容
public string ChoseContent { get; set; }
}
ObservableCollection <question> Questions = new ObservableCollection <question>();//题目数组 ObservableCollection<choseItem> ChoseItems = new ObservableCollection<choseItem>();//选项数组 private void Window_Loaded(object sender, RoutedEventArgs e)
{
string[] CharStr = new string[4] { "A", "B", "C", "D" };
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 4; j++)
{
choseItem item = new choseItem();//选项类
item.ChoseName = CharStr[j] + ":";
item.ChoseContent = "选项内容举例...";
ChoseItems.Add(item);
}
Questions.Add(new question(i, "__第" + (i + 1).ToString() + "题", ChoseItems));
}
datagrid1.ItemsSource = Questions; ShowChildCell(datagrid1, 2, 9);
} public static void ShowChildCell(DataGrid datagrid, int colNum, int childCellCount = 9)
{
DataGridTemplateColumn templeColumn = datagrid.Columns[colNum] as DataGridTemplateColumn;
if (templeColumn == null) return; DataGridCell dataGridCell = GetDataGridCell(datagrid, 0, colNum);
DataGridCellInfo dataGridCellInfo = new DataGridCellInfo(dataGridCell); object item = dataGridCellInfo.Item; FrameworkElement element = templeColumn.GetCellContent(item);
TextBox textBlockOther = (TextBox)templeColumn.CellTemplate.FindName("R1", element); textBlockOther.Visibility = Visibility.Hidden;
} public static DataGridCell GetDataGridCell(DataGrid datagrid, int rowIndex, int columnIndex)
{
try
{
DataGridRow rowContainer = GetDataGridRow(datagrid, rowIndex);
if (rowContainer != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
//这行代码是通过行得到单元格 DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
//这行代码是通过index得到具体的单元格 if (cell == null)
{
datagrid.ScrollIntoView(rowContainer, datagrid.Columns[columnIndex]);
cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
}
return cell;
}
}
catch
{
return null;
}
return new DataGridCell();
} public static DataGridRow GetDataGridRow(DataGrid dataGrid, int index)
{
if (index >= dataGrid.Items.Count)
{
throw new IndexOutOfRangeException(String.Format("Index {0} is out of range.", index));
} DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index);
if (row == null)
{
// may be virtualized, bring into view and try again
dataGrid.ScrollIntoView(dataGrid.Items[index]);
WaitFor(TimeSpan.Zero, DispatcherPriority.SystemIdle);
row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index);
} return row;
}
public static 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 static 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 static 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;
}
}
}

wpf dataGrid 获取单元格,并对单元格中的对象操作的更多相关文章

  1. WPF DataGrid 获取选中 一行 或者 多行

    WPF中DataGrid使用时,需要将其SelectedItem转换成DataRowView进行操作 然而SelectedItem 与SelectedItems DataGrid的SelectionU ...

  2. c# WPF DataGrid 获取选中单元格信息

    private void Dg_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e) { Console.Write ...

  3. WPF DataGrid 获取当前行某列值

    [0]是指当前行第1列的单元格位置 注意:DataRowView要求必须引用System.Data命名空间 方法一: DataRowView mySelectedElement = (DataRowV ...

  4. WPF DataGrid获取选择行的数据

    在WPF中,单击DataGrid,如何获取当前点击的行? 比如在MouseDoubleClick事件中,事实上获取的选中行是一个DataRowview,你可以通过以下的方法来获取选中行的数据,需要引用 ...

  5. WPF datagrid 获取行或单格为NULL 问题

    datagrid  属性 EnableRowVirtualization 设置为 false 解决...不要问我为什么. 害死我了

  6. 【转】WPF DataGrid 获取选中的当前行某列值

    方法一:DataRowView mySelectedElement = (DataRowView)dataGrid1.SelectedItem; string result = mySelectedE ...

  7. WPF DataGrid 获取选中的当前行某列值

    方法一: DataRowView mySelectedElement = (DataRowView)dataGrid1.SelectedItem; ]ToString(); 方法二: var a = ...

  8. wpf 前台获取资源文件路径问题

    1 <ImageBrush ImageSource="YT.CM.CommonUI;component/Resource/FloadwindowImage/middle.png&quo ...

  9. 获取wpf datagrid当前被编辑单元格的内容

    原文 获取wpf datagrid当前被编辑单元格的内容 确认修改单元个的值, 使用到datagrid的两个事件 开始编辑事件 BeginningEdit="dataGrid_Beginni ...

  10. WPF:获取DataGrid控件单元格DataGridCell

    转载:http://blog.csdn.net/jhqin/article/details/7645357 /* ------------------------------------------- ...

随机推荐

  1. Idea External Libraries 没有导入依赖

    Maven 下面是有依赖的,但是 Idea 的 External Libraries 没有导入进来,就非常奇怪,这个现象我在 Android Studio 也遇到过,要么找到 Maven 仓库,手动把 ...

  2. 重要内置函数、常见内置函数(了解)、可迭代对象、迭代器对象、for循环原理、异常捕获

    目录 一.重要内置函数 二.常见内置函数(了解) 三.可迭代对象 四.迭代器对象 五.for循环内部原理 六.捕捉异常 一.重要内置函数 1. zip 说白了就是压缩几组数据值,说细了就是将可迭代对象 ...

  3. ubuntu 启动脚本变化

    ubuntu-16.10 开始不再使用initd管理系统,改用systemd- 快速看了 systemd 的使用方法,发现改动有点大, 包括用 systemctl 命令来替换了 service 和 c ...

  4. 隐藏来源 禁用Referrer 的方法

    原文链接: https://www.cnblogs.com/duanweishi/p/16490197.html https://blog.csdn.net/qq996150938/article/d ...

  5. Altium Designer Winter 09 — 01 — 快速创建项目

    新建项目 新建原理图 导入所需的库 添加元器件和接插件 连接导线 自动标注.修改元件属性 编译前--修改项目属性 编译,查看消息 生成网表.BOM.简易BOM,打印文件

  6. NSIS Inetc插件 扩展使用

    Inetc客户端插件,用于文件的上传和下载. 官网文档:https://nsis.sourceforge.io/Inetc_plug-in 以下载net包为例 inetc::get "htt ...

  7. c++获取类型信息

    获取类型信息 typeid typeid运算符用来获取一个表达式的类型信息. 对于基本类型数据, 类型信息比较简单, 主要指数据的类型; 对于对象(类类型的数据), 类型信息指: 对象所属的类, 所包 ...

  8. Tomcat配置中的java.lang.IllegalStateException: No output folder问题

    最近运行Tomcat7.0时总会报错:Tomcat安装文件夹下的某个文件拒绝访问. localhost:8080 java.lang.IllegalStateException: No output ...

  9. Java面向对象之接口的定义与实现

    接口 普通类:只有具体实现 抽象类:具体实现和规范(抽象方法)都有! 接口:只有规范!自己无法写方法.专业的约束!约束和实现分离:面向接口编程 接口就是规范,定义的是一组规则,体现了现实世界中&quo ...

  10. 页面导出为PDF

    一.使用环境 Vue3.Quasar.Electron 二.安装 jspdf-html2canvas npm install jspdf-html2canvas --save 安装失败可以选择cnpm ...