[英]Expand row in WPF datagrid
我使用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,但是ActuallyHeight仍然是20。
附加信息:行的默認大小由DataGrid上的.RowHeight屬性設置。 我正在使用AutoGenerateColumns功能,並捕獲AutogeneratingColumn事件以使用DataGridTemplateColumn替換列。
為什么不用擴展器替換默認的DataGridCellTemplate來為你做所有這些?
<DataGridColumn>
<DataGridColumn.CellTemplate>
<DataTemplate>
<Expander Header="{Binding SomeText}">
<TextBlock TextWrapping="Wrap" Text="{Binding SomeText}" />
</Expander>
</DataTemplate>
</DataGridColumn.CellTemplate>
</DataGridColum>
如果你不喜歡默認的Expander外觀,你可以覆蓋它的模板看起來像一個普通的TextBlock
作為旁注,要拉伸和垂直對齊DataGridRow,您需要拉伸和對齊單元格內容,而不是行。
我嘗試使用擴展器,功能正是我想要的,但外觀並非如此。 我已經嘗試重新設置Expander來滿足我的需要,但是由於我需要添加它的事件而放棄了(XamlReader +事件超出了我的編程技能所能處理的)。 但根據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>
在代碼隱藏中,我可以處理事件“StackPanel_MouseUp”以更改TextBlocks的可見性。 這個控件的外觀和工作方式就像我希望重新設計的Expander一樣。
現在我的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.