1

I need to show the horizontal scrollbar when the width of the window is smaller than width of the text in first column.

<Window x:Class="Sample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="300"/> 
        <ColumnDefinition Width="80"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <ScrollViewer Grid.Column="0" >
        <TextBlock Text="Very looooong text" FontSize="30"/>
    </ScrollViewer>

    <Border Grid.Column="1" Background="Red"/>
    <Border Grid.Column="2" Background="Green"/>

</Grid>
</Window>

What's the best way to do it?

--edit When the user is dragging the right border of the window from right to left first I need to narrow the third column until it disappears, then I need to narrow second column until it disappears, and then when the width of window is smaller than text I need to show the horizontal scrollbar

4
  • 1
    To get a horizontal scroll bar, you need to set HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled" on your ScrollViewer. Commented Jan 25, 2016 at 21:32
  • @sthotakura, nope, it does not work Commented Jan 25, 2016 at 21:37
  • 1
    I tried in a sample app and it works.for me. You may need to increase the fontsize to something like 50 to see the effect? Commented Jan 25, 2016 at 21:43
  • Ok, when the fontsize is bigger your solution works, but I need solution, when the text is not wider than column at the beginning. I need to show scrollbar when user change the width of the window to smaller one. Commented Jan 25, 2016 at 21:58

1 Answer 1

2

You set the width of first column to a static value (300) so the column's width will not change when you resize the window. If you set a dynamic value, the ScrollViewer will work as you expect.

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="300*" MaxWidth="300"/>
        <ColumnDefinition MinWidth="80" MaxWidth="80"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <ScrollViewer Grid.Column="0" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Hidden" >
        <TextBlock Text="Very looooong text" FontSize="30"/>
    </ScrollViewer>

    <Border Grid.Column="1" Background="Red"/>
    <Border Grid.Column="2" Background="Green"/>

</Grid>

PS: Width of ScrollViewer must be less than the TextBlock, and the scrolling will work

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

5 Comments

Ok, that's true, but I need the static value of width in the first column, or at least I need the start value 'x' which means "not wider than 'x'". In your solution the first column stretches with whole window.
Okay, i edited my answer: <ColumnDefinition Width="*" MaxWidth="300"/> is the desired behavior. (also you can specify the MinWidth)
Wait, now I see that column with text becomes narrow simultaneously with the third column, which is not good. When the user is dragging the right border of window from right to left first I need to narrow the third column until it disappear, then I need to narrow second column, and then when the width of window is smaller than text I need to show the horizontal scrollbar
Edited again. It will behave as you described
Ok, that's it. Thanks

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.