Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can create a new item in the <code>DataSource</code> of the <code>ComboBox</code> which name is "ADD NEW ITEM" and handle when the user select it.</p> <pre><code>private void SelectItem(object sender, SelectionChangedEventArgs e) { if (e.AddedItems[0].ToString() == "new") { string newItem = "completely new item"; dataSource.Add(newItem); ((ComboBox)sender).SelectedItem = newItem; } } </code></pre> <p>In this question you can see a better example that each item is an instance of a class, so it's easier to handle the "add item" request:<br> <a href="https://stackoverflow.com/questions/4022118/databound-wpf-combobox-with-new-item">Databound WPF ComboBox with &#39;New...&#39; item</a></p> <hr> <p><strong>Edit</strong> <em>(about the 'add item' button template)</em>:<br> <em>Based on the example above</em></p> <p>Having this class</p> <pre><code>public class DisplayClass { public string Name { get; set; } public bool IsDummy { get; set; } } </code></pre> <p>You bind <code>ComboBox.ItemsSource</code> to an <code>ObservableCollection</code> like this one:</p> <pre><code>public ObservableCollection&lt;DisplayClass&gt; DataSource { get; set; } </code></pre> <p>Add that "dummy" item to the collection</p> <pre><code>DataSource.Add(new DisplayClass { Name = "ADD ITEM", IsDummy = true }); </code></pre> <p>Then you handle the item selection with something like this:</p> <pre><code>private void SelectItem(object sender, SelectionChangedEventArgs e) { var comboBox = (ComboBox)sender; var selectedItem = comboBox.SelectedItem as DisplayClass; if (selectedItem != null &amp;&amp; selectedItem.IsDummy) { //Creating the new item var newItem = new DisplayClass { Name = comboBox.Items.Count.ToString(), IsDummy = false }; //Adding to the datasource DataSource.Add(newItem); //Removing and adding the dummy item from the collection, thus it is always the last on the 'list' DataSource.Remove(selectedItem); DataSource.Add(selectedItem); //Select the new item comboBox.SelectedItem = newItem; } } </code></pre> <p>To display the items properly, you'll need to change the <code>ComboBox.ItemTemplate</code>, making the image invisible when the item is dummy</p> <pre><code>&lt;ComboBox ItemsSource="{Binding DataSource}" SelectionChanged="SelectItem"&gt; &lt;ComboBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBlock Text="{Binding Name}" Width="180" /&gt; &lt;Image HorizontalAlignment="Right" Source="..." MouseLeftButtonUp="DeleteItem"&gt; &lt;Image.Style&gt; &lt;Style TargetType="Image"&gt; &lt;Style.Triggers&gt; &lt;DataTrigger Binding="{Binding IsDummy}" Value="True"&gt; &lt;Setter Property="Visibility" Value="Hidden" /&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/Image.Style&gt; &lt;/Image&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/ComboBox.ItemTemplate&gt; &lt;/ComboBox&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload