如何在c#中为组合框项添加scroollbar

时间:2014-06-05 16:13:38

标签: c# silverlight combobox scrollbar silverlight-5.0

我正在使用c#处理 silverlight 。我必须在滚动条中显示组合项目。

我尝试这样做是:

            TextBlock txtblkName = generateTextBlock();
            ComboBox cb = new ComboBox();
            ScrollViewer scrollViewer = new ScrollViewer();

            cb.Width = 45;
            cb.Height = 20;
            foreach (String item in param.Component.Attributes.Items)
            cb.ItemsSource = param.Component.Attributes.Items;
            scrollViewer.Content = cb;
            scrollViewer.HorizontalAlignment = HorizontalAlignment.Center;
            scrollViewer.VerticalAlignment = VerticalAlignment.Center;
            scrollViewer.ScrollToVerticalOffset(3);
            cb.SelectionChanged += (o, e) =>
            {
                txtblkName.Text = cb.SelectedValue.ToString() + " " + param.Unit;
            };
            cb.SelectedIndex = param.Component.Attributes.Selected != -1 ? param.Component.Attributes.Selected : 0;
            Grid.SetColumn(scrollViewer, 1);
            childGrid.Children.Add(scrollViewer);

导致滚动组合框。就像这样:

enter image description here

不在滚动条显示的项目。 有人可以帮我创建仅在所有组合框上显示的项目滚动条吗?

1 个答案:

答案 0 :(得分:1)

这里你不需要ScrollViewer,如果你需要scobbar为你的comboboxItems设置这个属性 MaxDropDownHeight 到某个值

        ComboBox cb = new ComboBox();
        List<string> items = new List<string>();
        items.Add("1");
        items.Add("2");
        items.Add("3");
        items.Add("5");
        items.Add("7");
        items.Add("8");
        cb.ItemsSource = items;
        cb.MaxDropDownHeight = 20;
        childGrid.Children.Add(cb);