WPF:如何重置/重新计算ScrollViewer“ScrollableHeight”(WPF: How to reset / recalculate ScrollViewer “Scrollable

编程入门 行业动态 更新时间:2024-10-27 00:35:56
WPF:如何重置/重新计算ScrollViewer“ScrollableHeight”(WPF: How to reset / recalculate ScrollViewer “ScrollableHeight”)

背景:

我正在为设置页面生成UI。 设置存储在字典中,因为每个对象的设置都不同。

问题:

ScrollViewer的ScrollableHeight不符合内容的大小。 当ScrollViewer的内容更改时, ScrollableHeight不会重置,但会追加新内容的高度。

什么时候:

我在Grid生成内容, Grid是ScrollViewer的子元素。 内容为RowDefinitions ,其中名称 - 值对显示为TextBlocks和TextBoxes 。 选择其他对象以编辑其属性时,将清除网格的子项,并重新生成显示属性的UI。 正如我之前在问题定义中提到的,生成的内容的高度被附加到ScrollViewer的ScrollableHeight属性。

我学到:

我的第一个想法是清除ScrollViewer的ScrollableHeight并为添加的每一行追加行的高度以获得正确的大小。 问题是无法设置ScrollableHeight (私有设置器)。

码:

XAML:

<ScrollViewer Name="svScroller" Grid.Row="0"> <Grid x:Name="gdPropertyGrid" Margin="10"> <Grid.ColumnDefinitions> <ColumnDefinition Width="35*" /> <ColumnDefinition Width="65*" /> </Grid.ColumnDefinitions> </Grid> </ScrollViewer>

C#:

//Get Selected Item var listBox = ((ListBox)sender); var provider = listBox.SelectedItem as IProviderConfiguration; if (provider != null) { tbTitle.Text = String.Format("Properties for {0}",provider.Name); int rowCount = 0; PropertyGrid.Children.Clear(); //Get properties foreach (var property in provider.Properties) { //Create Grid Row var rowDef = new RowDefinition() {Height = new GridLength(30)}; PropertyGrid.RowDefinitions.Add(rowDef); //Create Name Label var tbPropertyName = new TextBlock { Text = property.Key, VerticalAlignment = VerticalAlignment.Center }; //Create Value input var tbPropertyValue = new TextBox { Text = property.Value.ToString(), VerticalAlignment = VerticalAlignment.Center }; //Add TextBlock & TextBox Grid PropertyGrid.Children.Add(tbPropertyName); PropertyGrid.Children.Add(tbPropertyValue); //Set Grid.Row Attached property Grid.SetRow(tbPropertyName, rowCount); Grid.SetRow(tbPropertyValue, rowCount); Grid.SetColumn(tbPropertyValue, 1); rowCount++; } }

Background:

I am generating the UI for a settings page. The settings are stored within a Dictionary as the settings will be different for each object in question.

Problem:

The ScrollableHeight of a ScrollViewer is not acurate to the size of the content. When the content of the ScrollViewer changes the ScrollableHeight is not reset, but appends the height of the new content.

When:

I am generating content within a Grid, which is a child element within the ScrollViewer. The content being RowDefinitions where name-value pairs are displayed as TextBlocks and TextBoxes. When a different object is selected in order to edit its properties, the Grid's Children are cleared and the UI to display the properties is regenerated. As I mentioned before in the problem defintion, the generated content's height is appended to the ScrollViewer's ScrollableHeight property.

What I have learned:

My first thought was to clear the ScrollViewer's ScrollableHeight and for each row added append the height of the row in order to achieve the correct size. The issue is that ScrollableHeight cannot be set (private setter).

Code:

XAML:

<ScrollViewer Name="svScroller" Grid.Row="0"> <Grid x:Name="gdPropertyGrid" Margin="10"> <Grid.ColumnDefinitions> <ColumnDefinition Width="35*" /> <ColumnDefinition Width="65*" /> </Grid.ColumnDefinitions> </Grid> </ScrollViewer>

C#:

//Get Selected Item var listBox = ((ListBox)sender); var provider = listBox.SelectedItem as IProviderConfiguration; if (provider != null) { tbTitle.Text = String.Format("Properties for {0}",provider.Name); int rowCount = 0; PropertyGrid.Children.Clear(); //Get properties foreach (var property in provider.Properties) { //Create Grid Row var rowDef = new RowDefinition() {Height = new GridLength(30)}; PropertyGrid.RowDefinitions.Add(rowDef); //Create Name Label var tbPropertyName = new TextBlock { Text = property.Key, VerticalAlignment = VerticalAlignment.Center }; //Create Value input var tbPropertyValue = new TextBox { Text = property.Value.ToString(), VerticalAlignment = VerticalAlignment.Center }; //Add TextBlock & TextBox Grid PropertyGrid.Children.Add(tbPropertyName); PropertyGrid.Children.Add(tbPropertyValue); //Set Grid.Row Attached property Grid.SetRow(tbPropertyName, rowCount); Grid.SetRow(tbPropertyValue, rowCount); Grid.SetColumn(tbPropertyValue, 1); rowCount++; } }

最满意答案

这是来自MSDN的ScrollViewer.ScrollableHeight的预期行为:

获取一个值,该值表示可以滚动的内容元素的垂直大小。

也许您正在寻找ScrollViewer.ViewPortHeight ? 或者也许你正在寻找ScrollViewer在滚动之前延伸到某一点。 在这种情况下,您需要查看另一种解决方案。

编辑

问题是您没有清除RowDefinitions,因此ScrollableHeight似乎总是附加到,因为您不断添加新行! 我的建议是你切换到使用另一个ListBox并使用Master-Detail模式。

That is the expected behavior of ScrollViewer.ScrollableHeight, from MSDN:

Gets a value that represents the vertical size of the content element that can be scrolled.

Perhaps you are looking for ScrollViewer.ViewPortHeight? Or maybe you're looking for the ScrollViewer to stretch until a certain point before scrolling. In that case you'll need to look at another solution.

EDIT

The bug is you do not clear the RowDefinitions, hence the ScrollableHeight always appears to be appended to, since you constantly add new rows! My suggestion is you switch to using another ListBox and go with a Master-Detail pattern.

更多推荐

本文发布于:2023-08-04 09:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415352.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ScrollViewer   WPF   ScrollableHeight   recalculate   reset

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!