Skip to main content

Microsoft Silverlight

Answered Question Can I scroll the listbox manually?RSS Feed

(0)

bertpu
bertpu

Member

Member

72 points

69 Posts

Can I scroll the listbox manually?

Hi,

Can I scroll the listbox using the code to scroll it to the top?

 Thanks.

SteveWong
SteveWong

Contributor

Contributor

6343 points

1,281 Posts

Re: Can I scroll the listbox manually?

_ListBox.ScrollIntoView(_ListBox.Items[0]);

 See if you have to change the selected Item to the first one too

you can do it by setting SelectedItem or SelectedIndex

Regards,
SteveWong (HongKong)
Please mark post as answer if they help you

Client App Dev

bertpu
bertpu

Member

Member

72 points

69 Posts

Re: Can I scroll the listbox manually?

It doesn't work. The scroll bar didn't move any more.

SteveWong
SteveWong

Contributor

Contributor

6343 points

1,281 Posts

Re: Re: Can I scroll the listbox manually?

Can I see your code?

You bind data to ListBox instead of adding Items, right?

Regards,
SteveWong (HongKong)
Please mark post as answer if they help you

Client App Dev

SteveWong
SteveWong

Contributor

Contributor

6343 points

1,281 Posts

Answered Question

Re: Re: Re: Can I scroll the listbox manually?

I have this as my CODE

C#

     public class _Item
    {
        public string Name { get; set; }
        public bool _IsChecked { get; set; }
    }

         List<_Item> _myStringList = new List<_Item>() {
                new _Item(){Name = "test1", _IsChecked = false},
                new _Item(){Name = "test2", _IsChecked = false},
                new _Item(){Name = "test3", _IsChecked = false},
                new _Item(){Name = "test4", _IsChecked = false},
                new _Item(){Name = "test5", _IsChecked = false},
                new _Item(){Name = "test6", _IsChecked = false},
                new _Item(){Name = "test7", _IsChecked = false},
                new _Item(){Name = "test8", _IsChecked = false},
                new _Item(){Name = "test9", _IsChecked = false},
                new _Item(){Name = "test10", _IsChecked = false},
                new _Item(){Name = "test11", _IsChecked = false},
                new _Item(){Name = "test12", _IsChecked = false},
                new _Item(){Name = "test13", _IsChecked = false},
                new _Item(){Name = "test14", _IsChecked = false},
                new _Item(){Name = "test15", _IsChecked = false},
                new _Item(){Name = "test16", _IsChecked = false},
                new _Item(){Name = "test17", _IsChecked = false},
                new _Item(){Name = "test18", _IsChecked = false},
                new _Item(){Name = "test19", _IsChecked = false},
                new _Item(){Name = "test20", _IsChecked = false}
            };

        public Page()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(Page_Loaded);
        }

        void Page_Loaded(object sender, RoutedEventArgs e)
        {           
            _ListBox.ItemsSource = _myStringList;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            _ListBox.ScrollIntoView(_ListBox.Items[0]);
        }

XAML:

        <StackPanel Orientation="Vertical" Height="300">
            <ListBox x:Name="_ListBox" Grid.Row="1" Grid.Column="1" Height="250" Margin="0,3,0,3" Width="250">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <CheckBox Content="{Binding Path=Name}" IsChecked="{Binding Path=_IsChecked, Mode=TwoWay}"/>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
            <Button Click="Button_Click" Height="30"/>
        </StackPanel>

Regards,
SteveWong (HongKong)
Please mark post as answer if they help you

Client App Dev
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities