静态设置包含List的索引后如何使ScrollViewer滚动? [英] How to get the ScrollViewer to scroll after statically set index containing List inside?

查看:124
本文介绍了静态设置包含List的索引后如何使ScrollViewer滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Silverlight 5和C#创建滚动条.

I am using Silverlight 5 and C# to create a scroll bar.

我有ListBox并创建了一个滚动条,并在该滚动条中显示了类似这样的项目列表:(我的代码显示了所有7个项目,但我只想显示3个项目,而没有通过滚动来滚动其余4个项目)

I am have ListBox and I create a scrollbar and inside that scrollbar I display a List of items like this: (My code shows all the 7 items but I just want to display 3 items without scrolling rest 4 by scrolling)

        TextBlock txtblkShowStatus = null;
        ListBox lines = new ListBox();
        ScrollViewer scrollViewer = new ScrollViewer();
        scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
        lines.ItemsSource = param.Component.Attributes.Items;
        scrollViewer.Content = lines;
        scrollViewer.HorizontalAlignment = HorizontalAlignment.Center;
        scrollViewer.VerticalAlignment = VerticalAlignment.Center;
        scrollViewer.ScrollToVerticalOffset(3); //By this line i want to display the only 3 items (out of7).
       //I mean the other 4 items must be visible on scrolling.

        Grid.SetColumn(scrollViewer, 1);          
        childGrid.Children.Add(scrollViewer);

        txtblkShowStatus = generateTextBlock();
        lines.SelectionChanged += (o, e) =>
        {
            txtblkShowStatus.Text = lines.SelectedItem.ToString();
        };
        lines.SelectedIndex = 2; //It will display énd item in txtblkShowStatus when no clikc happens at starting.
        Grid.SetColumn(txtblkShowStatus, 2);
        childGrid.Children.Add(txtblkShowStatus); //This childGrid contain a row with 3 columns.

通过这一行scrollViewer.ScrollToVerticalOffset(3);我只想显示7个项目中的3个.我的意思是其他4个项目在滚动滚动条时必须可见.

By this line scrollViewer.ScrollToVerticalOffset(3); i want to display only 3 items out of 7 .I mean the other 4 items must be visible on scrolling the scrollbar.

注意:请注意,我不必使用height,我需要处理索引,因为我将静态设置索引,并且它必须仅显示值,直到该索引和其余值将(如果您有其他想法可以实现,请对其进行解释).

NOTE: Please note that i don't have to use height , i need to deal with index because i will set the index statically and it must show just the values until that index and rest value will be displayed on scrolling. (if you have anyother idea to achieve it please explain it).

如何做到这一点?

推荐答案

这类似于rae1的建议

This is similar to rae1's suggestion,

        int index = 5; //say you want to display upto 5th element
        ListBox lines = new ListBox();
        lines.Width = 100;
        ScrollViewer scrollViewer = new ScrollViewer();
        scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
        for (int i = 0; i < 5; i++)
        {

            lines.Items.Add(new ListBoxItem
                        {
                           Content = i.ToString()
                        });
        }
        foreach (ListBoxItem lv in lines.Items)
        {
            lv.Height = 10;

        }
        scrollViewer.Height = index * 10;
        scrollViewer.Content = lines;
        Grid.SetColumn(scrollViewer, 1);
        childGrid.Children.Add(scrollViewer);

这篇关于静态设置包含List的索引后如何使ScrollViewer滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆