3

背景:

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

问题:

ScrollableHeightaScrollViewer对内容的大小不准确。当内容ScrollViewer更改时,ScrollableHeight不会重置,而是附加新内容的高度。

什么时候:

我在 a 中生成内容Grid,这是ScrollViewer. 内容是RowDefinitions名称-值对显示为TextBlocks和的位置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. 正如我之前在问题定义中提到的,生成的内容的高度附加到ScrollViewer'ScrollableHeight属性中。

我学到:

我的第一个想法是清除ScrollViewer'sScrollableHeight并为每一行添加附加行的高度以达到正确的大小。问题是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++;
    }

 }
4

2 回答 2

2

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

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

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

编辑

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

于 2009-08-31T13:04:19.477 回答
0

我同意@sixlettervariables,从您对问题的描述来看,您似乎正在尝试使用错误的属性做某事。我不清楚您想要的结果是什么,但是我假设您希望能够在同一个滚动查看器中拥有不同数量的项目,并根据需要扩展它以在运行时尽可能多地显示。

ScrollViewer.ViewPortHeight 允许您设置可见区域并且是一个读/写属性,并且当内容太大而无法容纳在可见区域中时,滚动条将(默认情况下)自动出现。

于 2009-08-31T13:17:10.037 回答