2011-10-25 79 views
1

我有一個Silverlight應用程序,其中ComboBoxVideoCaptureDevice填充。將項目添加到Silverlight組合框中

cbVideoDevices.ItemsSource = CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices(); 

我嘗試添加項目「選擇一個視頻設備」到第一個指標,但我不能得到它的工作。

XAML代碼:

<ComboBox Height="25" HorizontalAlignment="Left" Margin="0,0,0,0" Name="cbVideoDevices" VerticalAlignment="Top" Width="125" ItemsSource="{Binding AudioDevices}" SelectedItem="{Binding SelectedAudioDevice}"> 
     <ComboBox.ItemTemplate> 
      <DataTemplate> 
       <TextBlock Text="{Binding FriendlyName}"/> 
      </DataTemplate> 
     </ComboBox.ItemTemplate> 
    </ComboBox> 

回答

1

您在後面的代碼和XAML中明確設置了ItemsSource,選擇其中一個。理想情況下,您可以採用XAML方法並適當設置DataContext

做出該決定後,您可以使用Items屬性在ComboBox中插入一項。

ComboBox box = new ComboBox(); 
box.Items.Insert(0, "My Item"); 

一個更好的辦法是充分利用ICollectionView,只是對數據進行排序,並相應地讓UI響應。您的ItemsSource將被綁定到ICollectionView

0

您可以輕鬆地在使用下面的代碼的組合框的項目收集所需的索引位置插入項目。

  TextBlock t = new TextBlock(); 
     t.Text = "Select a video device" 
     combo.Items.Insert(0, t); 

設置所選的索引將設置組合框默認顯示您添加的項目:

combo.SelectedIndex = 0; 

你可以做這樣的..

YourClassObject objSelectItem = new YourClassObject(); 
    objSelectItem.ID = "0"; 
    objSelectItem.Name = "Select Item"; 
    ComboBox1.Items.Insert(0,objSelectItem); 

我希望它能幫助你...