在 WPF 数据网格中展开行
我正在使用 DataGrid 来显示一些日志文件,其中每个单元格都包含一个 TextBlock。我需要帮助创建一种方法来展开用户选择的行,如下所示:
这是我的代码 现在。它基于单击行的索引:
DataGridRow testrow = (DataGridRow)logBrowserDataGrid.ItemContainerGenerator.ContainerFromIndex(index);
logBrowserDataGrid.UpdateLayout();
logBrowserDataGrid.ScrollIntoView(logBrowserDataGrid.Items[index]);
testrow = (DataGridRow)logBrowserDataGrid.ItemContainerGenerator.ContainerFromIndex(index);
testrow.Height = 100;
但是这会产生一个奇怪的结果:
执行您知道基于索引扩展行的好方法吗?
你知道我得到的奇怪结果会发生什么吗?看起来我正在扩展该行的一部分,而其余部分确实伸展了。我还在运行时研究过它,可以看到它的高度是正确的 100,但实际高度仍然是 20。
附加信息: 行的默认大小由 DataGrid 上的 .RowHeight 属性设置。 我正在使用 AutoGenerateColumns 功能,并捕获 AutogenerateColumn 事件以用 DataGridTemplateColumn 替换该列。
I am using a DataGrid to display some log files, where each cell contains a TextBlock. I need help creating a method to expand a user selected row like this:
This is my code right now. It is based on the index of the clicked row:
DataGridRow testrow = (DataGridRow)logBrowserDataGrid.ItemContainerGenerator.ContainerFromIndex(index);
logBrowserDataGrid.UpdateLayout();
logBrowserDataGrid.ScrollIntoView(logBrowserDataGrid.Items[index]);
testrow = (DataGridRow)logBrowserDataGrid.ItemContainerGenerator.ContainerFromIndex(index);
testrow.Height = 100;
However this creates a weird result:
Do you know a god way to expand a row based on the index?
Do you know what happens in the weird result i get? It looks like i am expanding a part of the row, and the rest does stretch out. I have also studied it in runtime, and can see that its height is the correct 100, but the ActuallyHeight is still 20.
Additional info:
The default size of the rows are set by the .RowHeight property on the DataGrid.
I am using the AutoGenerateColumns feature, plus catching the AutogeneratingColumn event to replace the column with a DataGridTemplateColumn.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不使用 Expander 替换默认的 DataGridCellTemplate 来为您完成所有这些工作呢?
如果您不喜欢默认的 Expander 外观,您可以覆盖它的模板,使其看起来像纯 TextBlock
作为旁注,要拉伸和垂直对齐 DataGridRow,您需要拉伸和对齐单元格内容,而不是行。
Why not replace the default DataGridCellTemplate with an Expander to do all that for you?
If you don't like the default Expander look, you can overwrite it's Template to look like a plain TextBlock
As a side note, to stretch and vertically align a DataGridRow, you want to stretch and align the Cell Content, not the Row.
我尝试使用 Expander,其功能正是我想要的,但外观却并非如此。我曾尝试重新设计 Expander 以满足我的需要,但由于我需要添加事件而放弃(XamlReader + 事件超出了我的编程技能可以处理的范围)。但根据 Rachels 的建议,我创建了一个包含以下内容的 UserControl:
在代码隐藏中,我可以处理“StackPanel_MouseUp”事件来更改 TextBlock 的可见性。该控件的外观和工作方式就像我希望重新设计的 Expander 所做的那样。
现在我的 xaml 字符串看起来像这样。
I tried with an Expander and the functionality was precisely what I wanted, but the look was not however. I have tried restyling the Expander to fit my need, but gave up because of the events I needed to add to it (XamlReader + events was more than my programming skills could handle). But based on Rachels suggestion I made a UserControl with the following content:
In the codebehind I then can handle the event "StackPanel_MouseUp" to change the visibility of the TextBlocks. This control looks and works like I wanted the restyled Expander to do.
Now my xaml string looks like this.