Skip to main content
 首页 » 编程设计

c#之如何在 ComboBox 的开头和结尾显示 ComboBoxItem

2024年02月13日1298°冷暖

在我的 WinRT C#/XAML 应用程序中,我有一个 ComboBox,我在其中绑定(bind)到 List<T> .绑定(bind)按我想要的方式工作,但我希望有一个项目放在列表的开头,名为 Choose Chore 最后一个叫做 Add New 。我上面只有显示绑定(bind)列表:

<ComboBox x:Name="currentChore"  
          SelectedItem="Choose Chore"  
          DisplayMemberPath="Summary" 
          ItemsSource="{Binding ChoreList, Mode=TwoWay}"   
          SelectedValue="{Binding ChoreSingle, Mode=TwoWay}"> 
 
    <ComboBoxItem Content="Choose Chore" Foreground="Black" /> 
    <ComboBoxItem Content="Add New" Foreground="Black" /> 
</ComboBox> 

我该怎么做才能显示其中一项或两项?

请您参考如下方法:

使用 Collection<T>.Insert List<T> 的方法, 用于 ComboBox:

Inserts an element into the Collection at the specified index.

例子:

List<string> testList = new List<string>(); 
 
// Add in begin of List 
testList.Insert(0, "Choose Chore"); 
 
// Add in the end of List 
testList.Insert(testList.Count, "Add New item");