【问题标题】:How display a ComboBoxItem in beginning and in the ending in ComboBox如何在 ComboBox 的开头和结尾显示 ComboBoxItem
【发布时间】:2014-03-29 21:24:59
【问题描述】:

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

<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>

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

【问题讨论】:

  • 这是 WPF 还是 WinRT/XAML (Windows 8)?如果 WinRT/XAML - 您的目标是 Windows 8 还是 Windows 8.1?

标签: c# wpf mvvm combobox windows-runtime


【解决方案1】:

List&lt;T&gt;使用Collection&lt;T&gt;.Insert方法,用于ComboBox:

在指定索引处将元素插入到集合中。

例子:

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");

【讨论】:

    【解决方案2】:

    如果您的目标是 Windows 8.1 - PlaceholderText 可能是您用于“选择杂务”的东西,因为这不是可操作的 UI,当您点击/单击它时不会发生任何事情,这与您点击常规时发生的情况不同ComboBoxItem。至于“添加新项目”的事情 - 我建议在组合的一侧有一个单独的按钮,但如果你真的必须 - 你可以按照 Anatoliy 的建议,也许也可以使用 ItemTemplateSelector 来使用不同的 @该项目的 987654325@ 比组合中的所有其他项目。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-28
      • 1970-01-01
      • 1970-01-01
      • 2022-12-11
      • 2011-08-13
      • 2019-12-02
      • 1970-01-01
      相关资源
      最近更新 更多