我正在爲使用silverlight的窗口手機創建程序。使用C#代碼聲明組合框對象時的問題
我有一個小問題,它是關於建立一個組合框。
我可以使用XAML創建它,但我在的情況下,我應該用C#代碼申報。
的問題是創建組合框的一個新的實例和添加的項目之後,組合框不會出現!
代碼:
TextBlock tb = new TextBlock();
tb.Text = "Select your arrival status";
tb.Margin = new Thickness(5.0);
tb.FontSize = 20;
tb.Foreground = new SolidColorBrush(Colors.White);
ComboBox cb = new ComboBox();
ObservableCollection<string> testList = new ObservableCollection<string>();
testList.Add("Hi");
testList.Add("Hi1");
testList.Add("Hi2");
cb.DataContext = testList;
cb.Height = 50;
cb.Width = 200;
cb.Foreground = new SolidColorBrush(Colors.White);
panel.Children.Add(tb);
panel.Children.Add(cb);
正如你可以從代碼中看到,我宣佈一個TextBlock和一個組合框對象。 TextBox對象出現,而Combobox不出現。
任何幫助將不勝感激。
面板是什麼樣的元素?面板是一個抽象類,所以你的面板是一個Canvas,Grid,StackPanel?當他們加入孩子時,他們的行爲都不一樣。 – 2013-03-15 12:53:07
這是一個StackPanel – user2073081 2013-03-15 12:54:00
你給ComboBox命名了嗎?不確定它會做什麼,但值得一試。 – 2013-03-15 13:04:16