[英]Expand row in WPF datagrid
I am using a DataGrid to display some log files, where each cell contains a TextBlock. 我使用DataGrid显示一些日志文件,其中每个单元格包含一个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. 我也在运行时研究它,并且可以看到它的高度是正确的100,但是ActuallyHeight仍然是20。
Additional info: The default size of the rows are set by the .RowHeight property on the DataGrid. 附加信息:行的默认大小由DataGrid上的.RowHeight属性设置。 I am using the AutoGenerateColumns feature, plus catching the AutogeneratingColumn event to replace the column with a DataGridTemplateColumn. 我正在使用AutoGenerateColumns功能,并捕获AutogeneratingColumn事件以使用DataGridTemplateColumn替换列。
Why not replace the default DataGridCellTemplate with an Expander to do all that for you? 为什么不用扩展器替换默认的DataGridCellTemplate来为你做所有这些?
<DataGridColumn>
<DataGridColumn.CellTemplate>
<DataTemplate>
<Expander Header="{Binding SomeText}">
<TextBlock TextWrapping="Wrap" Text="{Binding SomeText}" />
</Expander>
</DataTemplate>
</DataGridColumn.CellTemplate>
</DataGridColum>
If you don't like the default Expander look, you can overwrite it's Template to look like a plain TextBlock 如果你不喜欢默认的Expander外观,你可以覆盖它的模板看起来像一个普通的TextBlock
As a side note, to stretch and vertically align a DataGridRow, you want to stretch and align the Cell Content, not the Row. 作为旁注,要拉伸和垂直对齐DataGridRow,您需要拉伸和对齐单元格内容,而不是行。
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). 我已经尝试重新设置Expander来满足我的需要,但是由于我需要添加它的事件而放弃了(XamlReader +事件超出了我的编程技能所能处理的)。 But based on Rachels suggestion I made a UserControl with the following content: 但根据Rachels的建议,我使用以下内容制作了UserControl:
<StackPanel Orientation="Vertical" MouseUp="StackPanel_MouseUp">
<TextBlock Name="headerTextBlock" Margin="3,2,3,2" Height="20" Width="Auto" TextWrapping="NoWrap"/>
<TextBlock Name="textTextBlock" Margin="3,2,3,2" Height="Auto" Width="Auto" TextWrapping="NoWrap" Visibility="Collapsed"/>
</StackPanel>
In the codebehind I then can handle the event "StackPanel_MouseUp" to change the visibility of the TextBlocks. 在代码隐藏中,我可以处理事件“StackPanel_MouseUp”以更改TextBlocks的可见性。 This control looks and works like I wanted the restyled Expander to do. 这个控件的外观和工作方式就像我希望重新设计的Expander一样。
Now my xaml string looks like this. 现在我的xaml字符串看起来像这样。
string xamlCellTemplateFormat =
@"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
~local~>
<local:CustomExpander x:Name=""UserControlTest"" Header=""{Binding Path=~binding~}"" Text=""{Binding Path=~binding~}""/>
</DataTemplate>";
string xamlCellTemplate = xamlCellTemplateFormat.Replace("~binding~", e.Column.Header.ToString());
xamlCellTemplate = xamlCellTemplate.Replace("~local~", " xmlns:local=\"clr-namespace:IS.AppFramework.Windows.LogBrowserWPF;assembly=" + Assembly.GetExecutingAssembly().GetName().Name + "\"");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.