0

As in title. I want to create TextBlock with both horizontal and vertical sliders, that will automatically adjust depending on text size. Google just shows me Slider control which is definitely not what i'm looking for.

Any clues what can i use to achieve it?

Edit

Thanks to some of the helpful people here i have this:

        <ScrollViewer Grid.Column="1" Style="{StaticResource MaterialDesignScrollViewer}">
            <TextBlock ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" Text="{Binding Path=(SQLLog:LogDisplay.LogAdvanced)}" FontSize="12"/>
        </ScrollViewer>

Vertical scroll bar appears, horizontal not. Even when text doesn't fit inside the TextBlock.

  • Remove `ScrollViewer.HorizontalScrollBarVisibility="Auto"` `ScrollViewer.VerticalScrollBarVisibility="Auto"` from the `TextBlock` and add it to your `ScrollViewer`. If you want horizontal scrolling to be possible also apply a value for `Width` to the `TextBlock` that is at least as big as the `ScrollViewer`s `Width`. – oRole May 21 '20 at 15:27
  • Now it works fine. Linked example had it inside `TextBlock`. Alright, i believe problem is solved. Thanks for help. –  May 21 '20 at 15:32

1 Answers1

1

You can use a ScrollViewer and its HorizontalScrollBarVisiblity and VerticalScrollBarVisibility properties. Just surround your TextBox with it:

<ScrollViewer HorizontalScrollBarVisibility="Auto" 
              VerticalScrollBarVisibility="Auto"
              Height="100"
              Width="200">
    <TextBlock Text="{Binding MyFancyTextProperty}" 
               Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ScrollViewer}}, Path=Width}"
               TextWrapping="Wrap"/>
</ScrollViewer>

Consider adding TextWrapping="Wrap" to your TextBlock, so that its content doesn't end up being displayed in one line.

enter image description here

If you want to display the scrollbars at any time, even if the content fits, set their values to Visible:

HorizontalScrollBarVisibility="Visible" 
VerticalScrollBarVisibility="Visible"
oRole
  • 1,316
  • 1
  • 8
  • 24
  • Horizontal scroll does not appear, even though it's set to auto. I'm trying to figure out whats wrong. –  May 21 '20 at 15:06
  • Edit your question and add the piece of code that doesn't work, so that we can help you. Hard to guess what you are doing. Normally you wouldn't need a horizontal scrollbar, if you let the content scroll vertically. – oRole May 21 '20 at 15:10