Franz Gsell
Franz Gsell

Reputation: 1565

Resize WPF TextBlock with WrapWithOverflow

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

Upvotes: 0

Views: 534

Answers (1)

mm8
mm8

Reputation: 169200

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.

Upvotes: 1

Related Questions