开发者

MVVM, XML, and Master/Detail Scenarios

I am working on my first MVVM project in C#, a conversion from a non MVVM project, and have questions around every corner. Although I can't share the exact data I'm using I have come up with a similar scenario that will help to address my questions.

Here is the data:

   <Stock>
      <Container Store="Store1"
                 Aisle="1"
                 Shelf="2"
                 Name="Box1">
         <Item Name="GreenBeans"/>
         <Item Name="Carrots"/>
         ...
      </Container>
      <Container Store="Store3"
                 Aisle="4"
                 Shelf="6"
                 Name="Box2">
         <Item Name="Pillow"/>
         <Item Name="Blanket"/>
         ...
      </Container>
      ...
   </Stock>

There is potential to be thousands of 开发者_C百科containers each with 10-15 items. It is possible for any two of the locators (Store/Aisle/Shelf) to be equivalent, but not all three (i.e. one container can exist in a certain location Store -> Aisle -> Shelf).

My current model clases are as below. These aren't the complete models, but this gives an idea of where I'm headed. It is possible, but not likely, that I will need to know all <Item\> at a particular Store or on a particular Aisle. With that said, is it imperative that I have those classes?

public class Store
{
   public String StoreName;
   public Aisles StoreAisles; 
}

public class Stores
{
   ObservableCollection<Store>
}

public class Aisle
{
   public int AisleNumber;
   public Shelfs AisleShelfs;
}

public class Aisles
{
   ObservableCollection<Aisle>
}

public class Shelf
{
  public int ShelfNumber;
  public Items ShelfItems;
}

public class Shelfs
{
   ObservableCollection<Shelf>
}

public class Item
{
   public string Name;
}

public class Items
{
   ObservableCollection<Item>
}

There will be three comboboxes (Stores, Aisles, and Shelfs) in a master/detail scenario. The data will be queried to tell me which store(s) are available, which aisle(s) at the store are being used, then which shelf(s) are in use on each aisle. Once <Container\> is established then the <Item(s)\> that are in that container will be bound to a user-control.

At this point, I am unsure of how to handle the data. Before going the MVVM route I was gathering the string from each SelectedItem and using those as inputs into a XElement Linq query. This would return a <Container\> in which I would then query the <Item(s)\>.

I want to take advantage of binding and avoid all the queries when I make changes to any of the comboboxes. How can I use binding to return the selected <Container\> to a property in the viewmodel?

All help and suggestions are welcome!


even if you use queries in the back you still could use binding (and why not) - you just have to fire the proper INotifyPropertyChanges as a result of one ComboBox-Changing (and with it the bound values in your ViewModel) - like this:

public string SelectedStoreName
{
   get { return _storeName; }
   set {
      if (!Equals(value, _storeName))
      {
         _storeName = value;
         _currentStore = FindStore(value);
         OnPropertyChanged("SelectedStoreName"); // invokes INotifyPropertyChanged
         OnPropertyChanged("AvaiableAisles");
      }
   }
}

public IEnumerable<Aisles> AvaiableAisles
{
    get { return _currentStore.StoreAisles.ToArray(); }
}

/* same with the rest of your chain */

Please Note a few things:

  • SelectedStoreName should be bound to your Store-Combobox if you don't like the string part change it (but I see no need)
  • AvaiableAisles should be bound to the Itemsource of your Aisles-Combobox - you will need a ItemTemplate or the right .ToString() overload in Aisles to show the content you like. If you don't like this approach change AvaiableAisles with a .Select(...) before the .ToArray() to select a string and change the signature into IEnumerable<string> AvaiableAisles
  • I did not mind any error handling for example using _currentStore
  • you have to Implemet INotifyPropertyChanged if you don't have allready
  • you have to implement the FindStore - this is where the query-part is
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜