tags:

views:

34

answers:

1

I'm working on my first true WPF MVVM pattern application.

Currently I have a number of ComboBoxes on various screens that are bound to Collection classes and properties of the relevant ViewModel class.

They always have an entry with the text <Add>, which is really an empty object class and I currently use it to trigger an AddNewObject event if the Property bound to the SelectedItem has <Add> in its ToString() output. This strikes me as cumbersome and it ties the View too closely to the View model for my liking. e.g.

<ComboBox ItemsSource="{Binding AllObjects}" SelectedItem="{Binding SelectedObject}" />

then in ViewModel code:

  public SomeObjectType SelectedObject
  {
    get{return this.fieldSomeObjectType;}
    set
    {
      if(null==value)
        return;
      if(value.ToString().Contains(@"<Add>"))
      {
        if(null!=this.AddNewObject)
        {
          this.AddNewObject;
        }
      }
    }
 }

Is there a way in XAML of adding this extra line into the ComboBox drop down list and binding it to an AddNewObject Command?

+1  A: 

The code you've written in your view has nothing to do with your business logic. Its fine. MVVM doesn't say that you shouldn't have anything in the codebehind of the view. Showing 'Add' is a requirement on the view and can be handled by the code behind of view.

In ASP.NET I've been doing this that I databinded the list control to some data but also specified some items in the html and it would merge them. Have you tried that?

Hasan Khan
Okay - that's worth knowing so +1, but is it possible to do this in XAML or would I be barking up the wrong tree - so to speak?
ChrisBD
In ASP.NET I've been doing this that I databinded the list control to some data but also specified some items in the html and it would merge them. Have you tried that?
Hasan Khan