在 WPF 数据网格中展开行

发布于 2024-12-16 18:44:28 字数 1106 浏览 0 评论 0原文

我正在使用 DataGrid 来显示一些日志文件,其中每个单元格都包含一个 TextBlock。我需要帮助创建一种方法来展开用户选择的行,如下所示:

http://dl.dropbox.com/u/5649690 /StackOverflow%20-%20Do%20not%20delete/Expand%20row%20in%20wpf%20datagrid/Do%20want.png

这是我的代码 现在。它基于单击行的索引:

DataGridRow testrow = (DataGridRow)logBrowserDataGrid.ItemContainerGenerator.ContainerFromIndex(index);

logBrowserDataGrid.UpdateLayout();
logBrowserDataGrid.ScrollIntoView(logBrowserDataGrid.Items[index]);

testrow = (DataGridRow)logBrowserDataGrid.ItemContainerGenerator.ContainerFromIndex(index);
testrow.Height = 100;

但是这会产生一个奇怪的结果:

http://dl.dropbox.com/u/5649690/StackOverflow%20-%20Do%20not%20delete/Expand%20row%20in%20wpf%20datagrid/Do%20not%20want%20.png

执行您知道基于索引扩展行的好方法吗?

你知道我得到的奇怪结果会发生什么吗?看起来我正在扩展该行的一部分,而其余部分确实伸展了。我还在运行时研究过它,可以看到它的高度是正确的 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:

http://dl.dropbox.com/u/5649690/StackOverflow%20-%20Do%20not%20delete/Expand%20row%20in%20wpf%20datagrid/Do%20want.png

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:

http://dl.dropbox.com/u/5649690/StackOverflow%20-%20Do%20not%20delete/Expand%20row%20in%20wpf%20datagrid/Do%20not%20want%20.png

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恋竹姑娘 2024-12-23 18:44:28

为什么不使用 Expander 替换默认的 DataGridCellTemplate 来为您完成所有这些工作呢?

<DataGridColumn>
    <DataGridColumn.CellTemplate>
        <DataTemplate>
            <Expander Header="{Binding SomeText}">
                <TextBlock TextWrapping="Wrap" Text="{Binding SomeText}" />
            </Expander>
        </DataTemplate>
    </DataGridColumn.CellTemplate>
</DataGridColum>

如果您不喜欢默认的 Expander 外观,您可以覆盖它的模板,使其看起来像纯 TextBlock

作为旁注,要拉伸和垂直对齐 DataGridRow,您需要拉伸和对齐单元格内容,而不是行。

Why not replace the default DataGridCellTemplate with an Expander to do all that for you?

<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

As a side note, to stretch and vertically align a DataGridRow, you want to stretch and align the Cell Content, not the Row.

凉世弥音 2024-12-23 18:44:28

我尝试使用 Expander,其功能正是我想要的,但外观却并非如此。我曾尝试重新设计 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”事件来更改 TextBlock 的可见性。该控件的外观和工作方式就像我希望重新设计的 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 + "\"");

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:

<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. This control looks and works like I wanted the restyled Expander to do.

Now my xaml string looks like this.

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 + "\"");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文