2014-06-05 27 views
0

我正在使用c#編寫silverlight。我必須在滾動條中顯示組合項目。如何將scroollbar添加到c中的組合框項目中#

我試圖做到這一點:

  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

你不需要的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); 
+0

Yaah它的工作。非常感謝Sajeentharan – Sss

+0

你似乎silverlight專家。請問我是否可以解決這個問題? http://stackoverflow.com/questions/24062877/how-to-get-the-scrollviewer-to-scroll-after-statically-set-index-containing-list/24063044?noredirect=1#comment37106250_24063044它仍然沒有回答正確; – Sss

+0

@ user234839 ok會檢查並更新,btw我不是專家 – Sajeetharan

相關問題