WPF: how to reset / recount ScrollViewer "ScrollableHeight"

Background:

I am creating a user interface for the settings page. Settings are saved in the Dictionary, because the settings for each object will be different.

Problem:

ScrollableHeight ScrollViewerDoesn't match the size of the content. When the content ScrollViewerchanges, do not reset, but adds the height of the new content. ScrollableHeight

When:

I am creating content inside Gridwhich is a child of in ScrollViewer. Content RowDefinitionswhere name-value pairs are displayed as TextBlocksand TextBoxes. When another object is selected for editing its properties, the children of the grid are cleared and the user interface for displaying properties is restored. As I mentioned earlier when defining the problem, the created content height is added to the property ScrollViewer ScrollableHeight.

What I learned:

My first thought was to clear ScrollViewer ScrollableHeight, and for each line added, add the height of the line to achieve the desired size. The problem is that ScrollableHeightit cannot be installed (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>

FROM#:

//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++;
    }

 }
+3
source share
2 answers

ScrollViewer.ScrollableHeight, MSDN:

, , .

, ScrollViewer.ViewPortHeight? , , ScrollViewer, . .

, RowDefinitions, ScrollableHeight , ! , ListBox Master-Detail.

+2

@sixlettervariables, , - . , , , , .

ScrollViewer.ViewPortHeight /, ( ) , , .

0

Source: https://habr.com/ru/post/1716487/


All Articles