Layout WPF ComboBox with the element "New ..."

Hello everyone | I have a combobox that binds to a list of items. But in addition to this list of items, I want to have another item. This element should display the text "New ..."
The idea is that if they select one of the usual elements, it performs some actions with this element. If they select the "New" element, it will display them on the screen where they can create a new element.
The problem is that when you databind something you won’t get the opportunity to add another element to it, and there is no question of adding a dummy element to the list of elements ...

Is it possible to create a new control based on ComboBox with the property "DefaultElement"? (with all related templates and command bindings, etc.)

+5
source share
3 answers

To do this, I previously created a dummy class for the regular type, allowing you to bind to a list containing basically the correct values, as well as your "New ...", for example,

public class DisplayClass
{
    public DisplayClass(ModelClass mc)
    {
         this.mc = mc;
    }

    public string Name
    {
        get { return this.mc != null ? this.mc.Name : "New..."; }
    }

    public bool IsDummy
    {
        return this.mc == null;
    }

    public ModelClass Model
    {
        return this.mc;
    }
}

(ViewModel) IsDummy. , , , , .

+5

ItemsSource CompositeCollection , - .

:

<ComboBox>
   <ComboBox.ItemsSource>
      <CompositeCollection>
         <ComboBoxItem>Add New Item...</ComboBoxItem>
         <CollectionContainer Collection="{Binding Source={StaticResource CollectionSource}}"/>
      </CompositeCollection>
   </ComboBox.ItemsSource>
</ComboBox>

MSDN CompositeCollection: http://msdn.microsoft.com/en-us/library/system.windows.data.compositecollection(v=vs.110).aspx

+3

, , , , - .

If I were you, I would enter the “new” entity in the first position of the linked collection and find it in my view model to trigger the corresponding action when the user selects it.

+2
source

All Articles