1

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.

2
  • 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 ScrollViewers Width.
    – oRole
    Commented May 21, 2020 at 15:27
  • Now it works fine. Linked example had it inside TextBlock. Alright, i believe problem is solved. Thanks for help.
    – user13555562
    Commented May 21, 2020 at 15:32

1 Answer 1

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"
2
  • Horizontal scroll does not appear, even though it's set to auto. I'm trying to figure out whats wrong.
    – user13555562
    Commented May 21, 2020 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
    Commented May 21, 2020 at 15:10

Your Answer

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