Question

I need to sync two scroll viewers in WPF, one of which is part of the TextBox item.

I intend on using the method described on CodeProject here:

However, this requires that I can attach attributes to both scrollviewers. One of the scrollviewers is the scrollviewer that comes as part of a textbox.

Code at the moment:

<ScrollViewer Core:ScrollSynchronizer.VerticalScrollGroup="V1">
    <UIComponents:LineNumberBox
            x:Name="LineBox"
            VisibleLines="{Binding ElementName=CodeBox, Path=(UIComponents:VisibleLinesBinder.VisibleLines)}"
            Padding="2,10,2,0"
            FontSize="14"
            Grid.Column="0"
            />
</ScrollViewer>

<UIComponents:SourceCodeBox 
        x:Name="CodeBox" 
        Padding="10" 
        FontSize="14" 
        Grid.Column="1"
        UIComponents:VisibleLinesBinder.ObserveVisibleLines="True"
        Core:ScrollSynchronizer.VerticalScrollGroup="V1"
    />

A UIComponents:SourceCodeBox is just a wrapper around a normal WPF Textbox.

Obviously the Core:ScrollSynchronizer.VerticalScrollGroup="V1" on the SourceCodeBox doesnt work? So how would I attach that attribute to the ScrollViewer within it? C# or XAML methods are both fine.

In case it makes any difference this is part of a User Control I am developing.

Was it helpful?

Solution

put the attatched behavior in a style like this:

        <UIComponents:SourceCodeBox 
                x:Name="CodeBox" 
                Padding="10" 
                FontSize="14" 
                Grid.Column="1"
                UIComponents:VisibleLinesBinder.ObserveVisibleLines="True"
                Core:ScrollSynchronizer.VerticalScrollGroup="V1">
        <UIComponents:SourceCodeBox.Resources>
            <Style TargetType="ScrollViewer">
                <Setter Property="Core:ScrollSynchronizer.VerticalScrollGroup" Value="V1"/>
            </Style>
        </UIComponents:SourceCodeBox.Resources>
    </UIComponents:SourceCodeBox>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top