当前位置: 代码迷 >> C# >> WPF,控件滚动到视图中,该如何解决
  详细解决方案

WPF,控件滚动到视图中,该如何解决

热度:591   发布时间:2016-05-05 05:32:36.0
WPF,控件滚动到视图中
在一个ScrollViewer钟,有很多控件,我知道如果要让ScrollViewer滚动的话,可以使用ScrollToHorizontalOffset(Double)方法、ScrollToVerticalOffset(Double)方法,但是,有的时候,页面布局是变化的,给这两个方法提供的Double值就会不太合适。
假如,我想指定让某个控件滚动到视图中,该怎么写更好呢,我使用控件自身调用BringIntoView方法,但是没有效果,滚动条不滚动。

该怎样用控件调用BringIntoView方法呢?

------解决思路----------------------
你的代码呢?贴出来
------解决思路----------------------
引用:
Quote: 引用:

你的代码呢?贴出来

代码太多了呢,ScrollViewer里面又有很多自定义控件,自定义控件里面又还有自定义控件,我是调用ScrollViewer里面某个自定义控件的控件模板中的,某个自定义控件的,....某个元素的BringIntoView方法。想让这个元素滚动到最外层的ScrollViewer的视图中

那就找到 某个元素的父控件。。。。直到找到自定义控件,再调用方法。
------解决思路----------------------
引用:
Quote: 引用:

Quote: 引用:

Quote: 引用:

你的代码呢?贴出来

代码太多了呢,ScrollViewer里面又有很多自定义控件,自定义控件里面又还有自定义控件,我是调用ScrollViewer里面某个自定义控件的控件模板中的,某个自定义控件的,....某个元素的BringIntoView方法。想让这个元素滚动到最外层的ScrollViewer的视图中

那就找到 某个元素的父控件。。。。直到找到自定义控件,再调用方法。

你的意思是,向上找到这个自定义控件,然后这个自定义控件调用BringIntoView方法?
这有什么用呢?我需要的是里面的某个小元素呈现出来



<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Button Width="120" Grid.Row="0" Height="30" Content="Scroll" Click="Button_Click"></Button>
        <ScrollViewer x:Name="myScrollViewer" Grid.Row="1">
            <WrapPanel Orientation="Vertical">
                <ListBox x:Name="mylb"  Height="300" Width="200" Background="Gray"></ListBox>
                <StackPanel x:Name="mysp" Height="200" Width="200" Background="Yellow">
                </StackPanel>
                <Label x:Name="myLabel" Height="100" Background="Red">hello</Label>
                <Border Width="200" Background="FloralWhite">
                    <Button x:Name="myChildBtn" Width="120" Height="30" Background="LightGoldenrodYellow">
                        <Label x:Name="mylbInner" Background="DarkSlateBlue"></Label>
                    </Button>
                </Border>
            </WrapPanel>
        </ScrollViewer>
    </Grid>
 private void Button_Click(object sender, RoutedEventArgs e)
        {
            mylbInner.BringIntoView();
        }

这是我的测试代码看看,控件要有固定的宽高。
  相关解决方案