Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF: How to reset / recalculate ScrollViewer "ScrollableHeight"
    primarykey
    data
    text
    <p><strong>Background:</strong></p> <p>I am generating the UI for a settings page. The settings are stored within a Dictionary as the settings will be different for each <em>object</em> in question.</p> <p><strong>Problem:</strong></p> <p>The <code>ScrollableHeight</code> of a <code>ScrollViewer</code> is not acurate to the size of the content. When the content of the <code>ScrollViewer</code> changes the <strong><code>ScrollableHeight</code> is not reset, but appends the height of the new content.</strong></p> <p><strong>When:</strong></p> <p>I am generating content within a <code>Grid</code>, which is a child element within the <code>ScrollViewer</code>. The content being <code>RowDefinitions</code> where name-value pairs are displayed as <code>TextBlocks</code> and <code>TextBoxes</code>. When a different <em>object</em> 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 <code>ScrollViewer</code>'s <code>ScrollableHeight</code> property.</p> <p><strong>What I have learned:</strong></p> <p>My first thought was to clear the <code>ScrollViewer</code>'s <code>ScrollableHeight</code> and for each row added append the height of the row in order to achieve the correct size. The issue is that <code>ScrollableHeight</code> cannot be set (private setter).</p> <p><strong>Code:</strong></p> <p>XAML:</p> <pre><code>&lt;ScrollViewer Name="svScroller" Grid.Row="0"&gt; &lt;Grid x:Name="gdPropertyGrid" Margin="10"&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="35*" /&gt; &lt;ColumnDefinition Width="65*" /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;/Grid&gt; &lt;/ScrollViewer&gt; </code></pre> <p>C#:</p> <pre><code>//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 &amp; 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++; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload