j3094531
j3094531

Reputation: 13

WPF ScrollViewer in grid's column

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

Upvotes: 1

Views: 2484

Answers (1)

stratever
stratever

Reputation: 596

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

Upvotes: 2

Related Questions