如何在 Silverlight 中的 DataGrid 末尾包含自定义行?
我的 Silverlight 应用程序中有一个 DataGrid
,它工作得很好,在我操作 ItemsSource
集合时添加一行或删除一行。 但是,我希望有一个附加行或控件始终出现在最后一个数据行之后。
我可以使用 ControlTemplate
并将 RowsPresenter 行设置为自动高度,让附加控件出现在最后一行之后,但这意味着当渲染区域变得太小时,行永远不会滚动。 但是,如果我将 RowsPresenter 行高度更改为星形,行会滚动,但附加控件会显示固定在数据网格的底部,而不是最后一行的底部。
有没有办法让 RowsPresenter 上的星形高度行为同时仍然让我的控件按我想要的方式显示?
我当前的想法是,我需要以某种方式使用 LoadingRow 事件来查找最后一行的位置,并使用 Canvas 或类似的控件将控件放置在适当的位置。
想法?
先谢谢您的帮助。
更新
我还问了一个关于将一个控件固定在另一个控件下方的问题(并最终回答),如果您不希望自定义行与其余行一起滚动(例如在我的情况下),则可以使用该问题来解决此问题,我想要另一个数据网格标题行来显示总计并浮动在其他行上)。
I have a DataGrid
in my Silverlight application and it works nicely, adding a row or removing a row as I manipulate the ItemsSource
collection. However, I want there to be an additional row, or control that always appears after the last data row.
I can get the additional control to appear after the last row using a ControlTemplate
and setting the RowsPresenter row to Auto height, but this means the rows never scroll when the render area gets too small. However, if I change the RowsPresenter row height to Star, the rows scroll but the additional control appears pinned to the bottom of the data grid rather than to the bottom of the last row.
Is there a way I can have the Star height behavior on the RowsPresenter while still having my control appear the way I want?
My current thinking is that I need to somehow use the LoadingRow event to find the position of the last row and use a Canvas or similar to place my control in the appropriate location.
Thoughts?
Thanks in advance for the help.
Update
I also asked a question (and ultimately answered) about pinning one control below another, which could be used to fix this issue if you don't want the custom row to scroll with the rest of the rows (such as in my case, where I wanted another datagrid header row to show totals and float over the other rows).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
昨晚我在灵感迸发的情况下解决了我的问题。 我注意到没有其他人投票支持这个问题,所以这个答案可能对任何人都没有帮助,但以防万一。
首先,我将自定义行控件和 RowsPresenter 组合在两行网格中,每行大小设置为“自动”。 然后,我将网格放置在 ScrollViewer 中,然后将滚动查看器行的大小调整为星形大小。 我没有将 VerticalScrollbar 模板部分添加到我的模板中,因为这只会滚动 RowsPresenter。
这为我提供了我正在寻找的确切行为,即添加行并且自定义行保持固定在最后一个数据行的底部。 当行和自定义行溢出可见区域的末端时,滚动条似乎允许滚动,同时保持标题固定到位。
任务完成。 我希望有人觉得这有帮助。 下面是我的 ControlTemplate XAML。
I solved my problem last night in a flurry of inspiration. I notice that no one else has voted for this question so this answer may not be helpful to anyone, but just in case.
First of all, I combined my custom row control and RowsPresenter in a grid of two rows, each row sized to Auto. I then placed the grid inside a ScrollViewer and then sized the scroll viewer row to Star sizing. I did not add the VerticalScrollbar template part into my template as this only scrolls the RowsPresenter.
This gave me the exact behaviour I was looking for where a row is added and the custom row remains pinned to the bottom of the last data row. When the rows and custom row overflow off the end of the visible area, the scrollbar appears to allow scrolling while keeping the headers fixed in place.
Job done. I hope someone finds this helpful. Below is my ControlTemplate XAML.
不确定这对 Silverlight 是否有帮助,但我通过添加名为 IsTotal 的不可见列,向 WPF DataGrid 添加了总计行。 我能够使用自定义分组/排序使这一行始终出现在网格的底部。 分组/排序顺序配置为使用此列作为主要排序,并具有固定方向。 看起来效果不错。
Not sure if this helps for Silverlight, but I added a totals row to a WPF DataGrid by adding and invisible column, called IsTotal. I was able to get this row to always appear at the buttom of the grid using custom grouping / sorting. The grouping / sort order was configured to use this column as the primary sort, with a fix direction. Seems to work well.
首先,为 DataGrid 和固定控件创建一个网格:
技巧是 VerticalAlignment="Top" - 当 DataGrid 小于可用高度时,它将移动到可用空间的顶部,固定控件将出现在其下方。
然后,将此网格放入一个垂直延伸的容器中,例如在另一个具有星形高度的网格的一行中:
您可能有任何其他垂直延伸的容器,而不是根网格,重要的是它会尝试填充所有可用空间。
First, create a Grid for the DataGrid and the pinned control:
The trick is VerticalAlignment="Top" - when the DataGrid is smaller than the available height, it will move to the top of the available space and the pinned control will appear under it.
Then, put this Grid into a container that stretches vertically, for example in a row of another Grid with Star height:
Instead of the root Grid you may have any other container that stretches vertically, the important thing is that it tries to fill all the available space for it.