开发者

Map complex UI to Object cleanly and efficiently

开发者 https://www.devze.com 2022-12-13 14:01 出处:网络
I currently have an app (in Silverligh开发者_高级运维t), using mv-vm, that has an interface to add/edit/remove various entries to a database. A good analogy would be a page for ordering a car with man

I currently have an app (in Silverligh开发者_高级运维t), using mv-vm, that has an interface to add/edit/remove various entries to a database. A good analogy would be a page for ordering a car with many checkboxes and comboboxes for whatever features you wanted the car to include.

My app has many of these UI elements (even including TreeViews with TreeViewItemCheckBox'es) that I want to map to a data object that uses a dictionary to store any included options.

My struggle is finding the best way to map these UI elements to my view model so that they can be compiled and sent to the database. My initial direction was to map each Element to a property in my view model, but it seems heavy and clumsy.

<TreeView>
   <TreeViewItem>
      <TreeViewItem.Header>
          <TreeViewItemCheckBox Content="All Features" IsChecked="{Binding AllFeaturesChecked}"/>
      </TreeViewItem.Header>
      <TreeViewItemCheckBox Content="Feature1" IsChecked="{Binding Feature1Checked}"/>
      <TreeViewItemCheckBox Content="Feature2" IsChecked="{Binding Feature2Checked}"/>
      <TreeViewItemCheckBox Content="Feature3" IsChecked="{Binding Feature3Checked}"/>
      <TreeViewItemCheckBox Content="Feature4" IsChecked="{Binding Feature4Checked}"/>
  </TreeViewItem>
</TreeView>

...multiply by 10 and you get the idea.

Can anybody think of a better way of going about this?

Thanks!


Why not have your ViewModels be responsible for this logic? It's compelling to make it very generic, but you are always going to run into some bit of complicated UI that doesn't fit in a dictionary quite right. Lots of edge cases. I would propose this:

public class BaseViewModel : ViewModel //Your basic viewmodel with INPC, etc
{
    public virtual void LoadModel(DictionaryTypeThing model) {}
    public virtual DictionaryTypeThing GetModel() {}
}

Here you a giving the responsibility of reading and creating those dictionaries to the ViewModel, which in this case is piece of your app with the most knowledge about how to piece everything together.

0

精彩评论

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