Marek M.
Marek M.

Reputation: 3951

Maintaining position of one of the controls inside ScrollViewer

Let's say I have XAML code like this:

<UserControl x:Class="Sample.MyClass"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="220" d:DesignWidth="750">
    <ScrollViewer Width="730" Height="150" CanContentScroll="True" HorizontalScrollBarVisibility="Visible">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="50" />
                <ColumnDefinition Width="680" />
            </Grid.ColumnDefinitions>

            <TextBox Width="50" Height="200" Grid.Row="0" Grid.Column="0" />
            <TextBox Width="680" Height="200" Grid.Row="0" Grid.Column="1" />
        </Grid>
    </ScrollViewer>
</UserControl>

Now when I scroll to the right I'd like the first TextBox to be fully visible. In other words - I would like the horizontal scrolling (only horizontal scrolling) to apply only to the second TextBox and vertical scrolling to aplly to both. I can't put the first one outside of the ScrollViewer because then vertical scrolling would not work on it.

To give you a more real-life example: In VisualStudio you have the text area where you can enter code. And on the left side there's a panel showing line numbers and code folding. If you scroll the text area vertically also the left panel is scrolling down or up. When you scroll the text area horizontally, only the text area is affected by it.

Upvotes: 1

Views: 326

Answers (2)

Dave
Dave

Reputation: 109

I think what you want to do is svnc 2 scroll viewers,

You can do it with a little code behind voodoo, check this out

Synchronized scrolling of two ScrollViewers whenever any one is scrolled in wpf

Upvotes: 1

Alexander Bell
Alexander Bell

Reputation: 7918

You can try to modify XAML as following:

<UserControl x:Class="Sample.MyClass"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="220" d:DesignWidth="750">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="50" />
            <ColumnDefinition Width="680" />
        </Grid.ColumnDefinitions>
        <ScrollViewer Grid.Row="0" Grid.Column="0" 
                  CanContentScroll="True" 
                  VerticalScrollBarVisibility="Visible" 
                  HorizontalAlignment="Stretch">
            <TextBox  />
       </ScrollViewer>
       <ScrollViewer Grid.Row="0" Grid.Column="1"  
                  CanContentScroll="True" 
                  VerticalScrollBarVisibility="Visible" 
                  HorizontalScrollBarVisibility="Visible" 
                  HorizontalAlignment="Stretch" 
                  VerticalAlignment="Stretch">
            <TextBox />
        </ScrollViewer>
    </Grid>
</UserControl>

Hope this may help.

Upvotes: 1

Related Questions