0

xaml

<Window x:Class="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">
<ScrollViewer x:Name="ScrollViewer1" CanContentScroll="True" SnapsToDevicePixels="True" VerticalScrollBarVisibility="Visible">
    <DockPanel LastChildFill="False">
        <RadioButton x:Name="RadioButton1" Content="RadioButton1" GroupName="MyGroup" Width="100" DockPanel.Dock="Top" Margin="30"/>
        <RadioButton x:Name="RadioButton2" Content="RadioButton2" GroupName="MyGroup" Width="100" DockPanel.Dock="Top" Margin="30"/>
        <RadioButton x:Name="RadioButton3" Content="RadioButton3" GroupName="MyGroup" Width="100" DockPanel.Dock="Top" Margin="30"/>
        <RadioButton x:Name="RadioButton4" Content="RadioButton4" GroupName="MyGroup" Width="100" DockPanel.Dock="Top" Margin="30"/>
        <RadioButton x:Name="RadioButton5" Content="RadioButton5" GroupName="MyGroup" Width="100" DockPanel.Dock="Top" Margin="30"/>
        <RadioButton x:Name="RadioButton6" Content="RadioButton6" GroupName="MyGroup" Width="100" DockPanel.Dock="Top" Margin="30"/>
        <RadioButton x:Name="RadioButton7" Content="RadioButton7" GroupName="MyGroup" Width="100" DockPanel.Dock="Top" Margin="30"/>
        <RadioButton x:Name="RadioButton8" Content="RadioButton8" GroupName="MyGroup" Width="100" DockPanel.Dock="Top" Margin="30"/>
    </DockPanel>
</ScrollViewer>
</Window>

vb.net

Class MainWindow 
Private Sub RadioButton5_Checked(sender As Object, e As RoutedEventArgs) Handles RadioButton5.Checked
    ScrollViewer1.ScrollToVerticalOffset(0)
End Sub
End Class

If you click RadioButton5 you will see that ScrollViewer1 scrolls to the zero position. So, the above codes work great.

I want ScrollViewer1 to scroll to the RadioButton3 position when The User clicks RadioButton8.

The answer from Robert Levy looks good but I don't understand how to implement Robert Levy's answer here: How to scroll WPF ScrollViewer's content to specific location

Hmax
  • 314
  • 6
  • 16

1 Answers1

0

If you want to scroll the button to be under the mouse: You can get the position of the button relative to the mouse with

Point position = Mouse.GetPosition(RadioButton5);

And then scroll to the button with

ScrollViewer1.ScrollToVerticalOffset(ScrollViewer1.VerticalOffset - position.Y);

If I understand you correctly you wand to scroll the button to be on top of the list. You could reach this by

Point point = ScrollViewer1.TranslatePoint(new Point(), RadioButton5);
ScrollViewer1.ScrollToVerticalOffset(ScrollViewer1.VerticalOffset - point.Y);

I hope this is what you tried to do.

robins
  • 56
  • 4