簡體   English   中英

如何在C#中將scroollbar添加到組合框項目

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

我正在使用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);

導致在組合框上滾動。像這樣:

在此處輸入圖片說明

不在滾動條上顯示的項目上 有人可以幫我僅在未在所有組合框中顯示的項目上創建滾動條嗎?

您在這里不需要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