如何在C#中为ListBox创建滚动条

时间:2014-05-27 16:06:02

标签: c# silverlight listbox scrollbar scrollviewer

我正在使用 silverlight 并在c#中编码,我必须在显示的列表上创建滚动条。

目前列表显示时没有滚动条,但它也应包含一个滚动条。

我显示列表的代码是:

ListBox lines = new ListBox(); 
ScrollViewer scrollViewer = new ScrollViewer();            
scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
lines.ItemsSource = param.Component.Attributes.Items;
Grid.SetColumn(lines, 1);
Grid.SetRow(lines, LoopCount);
childGrid.Children.Add(lines);

lines.SelectedIndex = 0;
lines.SelectedItem = param.Component.Attributes.Items;
lines.SelectionChanged += new SelectionChangedEventHandler(List_SelectionChanged);
lines.SelectedIndex = lines.Items.Count - 1;

"线"变量包含我的ListBox,我试图通过scrollViewer 实现它,但我不知道如何继续进行。 (请注意,我必须在选择按钮单击时显示的GUI上的项目时创建选择更改事件),因此滚动条的代码不得影响该过程。而且我必须在c#中编码,而不是在xaml中。在此先感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

当您是puttig项目内 ScrollViewer 您需要将ScrollViewer的内容设置为ListBox,

更改您的代码,如下所示,

//Create ScrollViewer which is a child of Grid
var scroll = new ScrollViewer();
//add to the grid
childGrid.Children.Add(scroll);
//create lisbox and set it to the content of scrollviewer which is a Child of ScrollViewer
var listBox = new ListBox();
scroll.Content = listBox;