Sss
Sss

Reputation: 1529

How to get the ScrollViewer to scroll after statically set index containing List inside?

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

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.

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.

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).

How to do the achieve this?

Upvotes: 0

Views: 447

Answers (2)

Sajeetharan
Sajeetharan

Reputation: 222592

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);

Upvotes: 1

rae1
rae1

Reputation: 6144

The ScrollToVerticalOffset method does not do what you are trying to use it for. It will scroll only when there is a need to. In your case you are seeing all 7 elements because the space allows you to see them.

If you want to only show the first three items you need to modify the Height property for the ScrollViewer and set it to a proper value.

If you have 7 items, and each items is 10px in height, and the height of the ScrollViewer is 100px there is not need to scroll because all items can be fitted in the space given. However, if you change the height to 30px, then the ScrollViewer only has 30px to display a 70px content size, and thus you need to scroll to see the rest of the items, achieving the effect you are after.

Upvotes: 1

Related Questions