Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF Scrollviewer DesiredSize does not increase when ScrollBar gets Visible
    text
    copied!<p>I have a WPF <code>UserControl</code> (inside an <code>ElementHost</code>) with a <code>ScrollViewer</code> that contains an <code>ItemsControl</code> . The <code>HorizontalScrollbarVisibility</code> is set to <code>Auto</code>, so if no scrolling is necessary, the <code>ScrollBar</code>gets hidden.</p> <p>My requirement is, that if the <code>ScrollBar</code> gets shown/hidden, the <code>ElementHost</code> does adjust it's height accordingly. To achieve that, I'm listening to the <code>SizeChanged</code> event, I get the <code>DesiredSize</code> of the <code>ScrollViewer</code> in the <code>EventHandler</code>, then I pass <code>DesiredSize.Height</code> to the <code>ElementHost</code>.</p> <ol> <li><img src="https://i.stack.imgur.com/03dFE.png" alt="visible"> 2. <img src="https://i.stack.imgur.com/Xsejo.png" alt="hidden"> 3. <img src="https://i.stack.imgur.com/cn4Xu.png" alt="incorrect, overlaid"></li> </ol> <p>One way, this works: With <code>ScrollBar</code> visible (situation 1), I enlarge my window until all items of the <code>ItemsControl</code> are visible, the <code>ScrollBar</code> disappears, the <code>ElementHost</code> adjusts to reduced height (situation 2). <code>DesiredSize</code> actually got smaller the moment the <code>ScrollBar</code> is hidden.</p> <p>The other way, though, it doesn't work: With <code>ScrollBar</code> not visible (situation 2), I reduce my window size until a <code>ScrollBar</code> is necessary and appears. <code>DesiredSize</code> stays the same, and the <code>ElementHost</code> does not adjust (situation 3).</p> <p>Any ideas?</p> <p>This is the xaml of the <code>Scrollviewer</code>, with some MVVM stuff, but don't get hung up on this, the point really is, why does the <code>DesiredSize</code> not increase when the <code>ScrollBar</code> appears? Why is it shrink only?</p> <pre><code>&lt;ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Hidden" &gt; &lt;i:Interaction.Behaviors&gt; &lt;beh:HeightChangedBehavior HeightChangedCommand="{Binding HeightChangedCommand}" /&gt; &lt;/i:Interaction.Behaviors&gt; &lt;ItemsControl ItemsSource="{Binding TabHeaders}" &gt; &lt;ItemsControl.ItemsPanel&gt; &lt;ItemsPanelTemplate&gt; &lt;StackPanel IsItemsHost="True" Orientation="Horizontal"/&gt; &lt;/ItemsPanelTemplate&gt; &lt;/ItemsControl.ItemsPanel&gt; &lt;ItemsControl.ItemTemplate&gt; &lt;DataTemplate DataType="models:TabHeaderButtonModel"&gt; &lt;RadioButton Content="{Binding Caption}" IsChecked="{Binding IsChecked, Mode=TwoWay}" GroupName="Tabs" Command="{Binding SelectionChangedCommand}" CommandParameter="{Binding}" Style="{StaticResource TabHeaderToggleButtonStyle}"&gt; &lt;/RadioButton&gt; &lt;/DataTemplate&gt; &lt;/ItemsControl.ItemTemplate&gt; &lt;/ItemsControl&gt; &lt;/ScrollViewer&gt; </code></pre> <p>ScrollViewer style (basically default WPF):</p> <pre><code>&lt;Style x:Key="ScrollViewerStyle1" TargetType="{x:Type ScrollViewer}"&gt; &lt;Setter Property="Template" &gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="{x:Type ScrollViewer}"&gt; &lt;Grid x:Name="Grid" Background="{TemplateBinding Background}"&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="*"/&gt; &lt;ColumnDefinition Width="Auto"/&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="*"/&gt; &lt;RowDefinition Height="Auto"/&gt; &lt;/Grid.RowDefinitions&gt; &lt;Rectangle x:Name="Corner" Grid.Column="1" Fill="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" Grid.Row="1"/&gt; &lt;ScrollContentPresenter x:Name="PART_ScrollContentPresenter" CanContentScroll="{TemplateBinding CanContentScroll}" CanHorizontallyScroll="False" CanVerticallyScroll="False" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Grid.Column="0" Margin="{TemplateBinding Padding}" Grid.Row="0"/&gt; &lt;ScrollBar x:Name="PART_VerticalScrollBar" AutomationProperties.AutomationId="VerticalScrollBar" Cursor="Arrow" Grid.Column="1" Maximum="{TemplateBinding ScrollableHeight}" Minimum="0" Grid.Row="0" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportHeight}"/&gt; &lt;ScrollBar x:Name="PART_HorizontalScrollBar" AutomationProperties.AutomationId="HorizontalScrollBar" Cursor="Arrow" Grid.Column="0" Maximum="{TemplateBinding ScrollableWidth}" Minimum="0" Orientation="Horizontal" Grid.Row="1" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportWidth}" Style="{DynamicResource ScrollBarStyle1}"/&gt; &lt;/Grid&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;Style.Triggers&gt; &lt;Trigger Property="IsEnabled" Value="false"&gt; &lt;Setter Property="Foreground" Value="White"/&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; </code></pre>
 

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