0

I have a textblock on a window which is contained in a scroll viewer. For the textlblock I have set the text wrapping to "WrapWithOverflow". In addition I bound the textblock width property to the actual width of the scroll viewer.

My desired behavior is, that I can resize my window and the textblock should wrap the content. The window should only show the scrollbars when other controls (e.g. the buttons in the example xaml get cut)

Xaml:

<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
    <DockPanel Margin="5">
        <StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
            <Button Content="First Button" Margin="0,0,10,0"/>
            <Button Content="Second Button"/>
        </StackPanel>
        <DockPanel>
            <TextBlock VerticalAlignment="Center" IsHitTestVisible="False" TextAlignment="Center" TextWrapping="WrapWithOverflow" 
                   Width="{Binding RelativeSource={RelativeSource AncestorType=ScrollViewer}, Path=ActualWidth}" MaxWidth="260">
            Just a small line<LineBreak />
            This is the long line which will wrap during resize</TextBlock>
        </DockPanel>
    </DockPanel>
</ScrollViewer>

But I see the scroll bars even befor the buttons get cut. I guess this is because of the margin in the dockpanel which is required in my case.

enter image description here

1
  • Why is the margin "required in your case"? Commented Jan 30, 2020 at 15:23

1 Answer 1

1

I guess this is because of the margin in the dockpanel

Correct.

...which is required in my case.

Why? You should move the Margin to the StackPanel:

<DockPanel>
    <StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Margin="5">
    ...

...or the buttons:

<DockPanel>
    <StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
        <Button Content="First Button" Margin="5,5,5,5"/>
        <Button Content="Second Button" Margin="5,5,0,5"/>
    </StackPanel>
    ...

This is necessary as the margins are included in the ActualWidth that you bind to.

Sign up to request clarification or add additional context in comments.

2 Comments

So this means if there are many stackpanels and other controls I have to apply the margin to all controls instead of applying one margin for all?
@FranzGsell: Well, depending on your layout and scrolling requirements, yes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.