C# 如何将ScrollViewer滚动到特定的单选按钮位置

C# 如何将ScrollViewer滚动到特定的单选按钮位置,c#,wpf,vb.net,C#,Wpf,Vb.net,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" CanC

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>
如果单击
RadioButton5
,您将看到
ScrollViewer1
滚动到零位置。因此,上述代码非常有效

当用户单击
RadioButton8
时,我希望
ScrollViewer1
滚动到
RadioButton3
位置


罗伯特·列维的答案看起来不错,但我不知道如何实现罗伯特·列维的答案:

如果你想滚动鼠标下的按钮: 您可以使用以下命令获取按钮相对于鼠标的位置:

Point position = Mouse.GetPosition(RadioButton5);
然后滚动至带有的按钮

ScrollViewer1.ScrollToVerticalOffset(ScrollViewer1.VerticalOffset - position.Y);
如果我理解正确,您可以滚动按钮,使其位于列表顶部。你可以通过电话联系到这个

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

我希望这就是您试图做的。

您是否尝试将按钮置于滚动浏览器顶部?
Point point = ScrollViewer1.TranslatePoint(new Point(), RadioButton5);
ScrollViewer1.ScrollToVerticalOffset(ScrollViewer1.VerticalOffset - point.Y);