MVVM、XML 和主/详细信息场景

发布于 2024-12-09 07:14:37 字数 1905 浏览 0 评论 0原文

我正在用 C# 开发我的第一个 MVVM 项目,这是一个从非 MVVM 项目的转换,并且在每个角落都有问题。尽管我无法分享我正在使用的确切数据,但我提出了一个类似的场景,这将有助于解决我的问题。

数据如下:

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

可能有数千个集装箱,每个集装箱内装有 10-15 件物品。任何两个定位器(商店/过道/货架)可能是等效的,但不是所有三个(即一个容器可以存在于某一位置商店->过道->货架)。

我当前的模型类如下。这些不是完整的模型,但这让我知道了我的前进方向。我有可能(但不太可能)需要了解特定商店或特定过道上的所有 。话虽如此,我有必要上这些课吗?

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

主/详细场景中将存在三个组合框(商店、过道和货架)。将查询数据来告诉我哪些商店可用,商店的哪些过道正在使用,然后每个过道上哪些货架正在使用。一旦建立,则该容器中的将绑定到用户控件。

此时,我不确定如何处理数据。在采用 MVVM 路线之前,我从每个 SelectedItem 收集字符串,并将它们用作 XElement Linq 查询的输入。这将返回一个 ,然后我将在其中查询

当我对任何组合框进行更改时,我想利用绑定并避免所有查询。如何使用绑定将选定的 返回到视图模型中的属性?

欢迎所有帮助和建议!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

dawn曙光 2024-12-16 07:14:37

即使您在后面使用查询,您仍然可以使用绑定(为什么不呢) - 您只需由于一个 ComboBox-Changing(以及 ViewModel 中的绑定值)而触发适当的 INotifyPropertyChanges - 像这样:

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

请注意一些事情:

  • 如果您不喜欢字符串部分,则应将 SelectedStoreName 绑定到您的 Store-Combobox(但我认为没有必要)
  • AvaiableAisles 应绑定到 Aisles-Combobox 的 Itemsource - 您需要在 Aisles 中使用 ItemTemplate 或正确的 .ToString() 重载显示您喜欢的内容。如果您不喜欢这种方法,请在 .ToArray() 之前使用 .Select(...) 更改 AvaiableAisles 以选择字符串并将签名更改为 IEnumerable; AvaiableAisles
  • 我不介意任何错误处理,例如使用 _currentStore
  • 你必须实现 INotifyPropertyChanged 如果你还没有准备好,
  • 你必须实现 FindStore - 这是查询部分所在的位置

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文