简体   繁体   English

如何在C#中将scroollbar添加到组合框项目

[英]How to add scroollbar to combo box items in c#

I am working on silverlight using c#. 我正在使用C#开发Silverlight I have to display the combo items in a scrollbar. 我必须在滚动条中显示组合项。

My attempt to do this is: 我的尝试是:

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

which results in scroll over combo box .Like this: 导致在组合框上滚动。像这样:

在此处输入图片说明

Not on it's item displayed by scrollbar . 不在滚动条上显示的项目上 Could some one please help me to create scrollbar only on items displayed not on all combo box? 有人可以帮我仅在未在所有组合框中显示的项目上创建滚动条吗?

You dont need ScrollViewer here, If you need Scrollbar for your comboboxItems set this property MaxDropDownHeight to some value 您在这里不需要ScrollViewer,如果您的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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM