c# - MVVM and DataTemplates -


i had asked question mapping multiple view models single view (here). got answers still having trouble applying learned there particular case.

a brief recap: want create base itemviewmodelbase class exposes properties view bind to. create 2 specific view models, peopleviewmodel , carsviewmodel. both of these inherit itemviewmodelbase , implement appropriate properties. now, want able create single view display appropriate info based on view model bound to. since both peopleviewmodel , carsviewmodel expose same properties , want view same both of these, need 1 view.

one of answers in previous question suggested using datatemplate:

<datatemplate datatype="{x:type itemviewmodelbase}">    //some user control </datatemplate> 

i new using datatemplates mvvm (and mvvm in general) have few questions:

right itemviewmodelbase abstract class , defined appropriate properties (itemname, items, etc.). items property observablecollection:

public virtual observablecollection<???> items { get; set; } 
  • what put collection type? classes derive base class have different lists (person, car). base view model right place put property? want of derived classes implement seems so. , doesn't make sense have person , car extend base object.

  • let's not need customization of views. need 1 view in case. not clear how set up. should create datatemplate itemviewmodelbase , single view (user control) represent it? right use unity register view models , when view created, view model gets injected in view. how differentiate between different view models when try create view?

basically, don't know how show appropriate view when using datatemplates. in application right have window contains tab control defined this:

<grid>     <tabcontrol tabstripplacement="left" itemssource="{binding tabitems}"/> </grid> 

the tabcontrol's style contains below setters:

<setter property="header" value="{binding header}"/> <setter property="content" value="{binding content}"/> 

tabitems defined so:

public observablecollection<configtabitem> tabitems { get; set; }  tabitems.add(new configtabitem() { header = "people", resolveview = (func<object>)(() => (peopleview)container.resolve(typeof(peopleview), "peopleview")) }); tabitems.add(new configtabitem() { header = "cars", resolveview = (func<object>)(() => (carsview)configurationmodule.container.resolve(typeof(carsview), "carsview")) }); 

so stands right now, have separate view models , views people , cars, , whenever tab clicked, appropriate view resolved.

i want change setup use above mentioned base view model class , single view datatemplates.

any sample code/sample appreciated, showing base view model class, other view model classes extending base view model, , being able show appropriate view based on view model (where there 1 generic view).

you asking in single question imho. try getting code work or without datatemplates (make hacky if need to) , focus on 1 area of code think needs refinement, , post question how solve specific problem. started typing out answer , got complicated articulate overall recommendation.

i'm not sure got advice in other question datatemplates. i'm concerned how have configtabitem set up--it seems parent viewmodel uses container (directly, not practice) resolve view, presumably has own viewmodel. seems needlessly complicated.

anyway, again, try distill down few focused questions. in case of helpful, original start @ answer below (unedited):


first, i'm not sure understand answer given in previous question talks datatemplates. if binding itemviewmodelbase, i'm not clear on why need specify datatype (or datatemplate). not using datatemplate bad idea, i'm not sure see necessity of in case.

i'm going i'm not sure see necessity of inheritence either. databinding works @ runtime, , outside of switching datatemplate based on vm type (which, said, don't think need), view doesn't care binding to, long properties looking found @ runtime.

so general matter, start 1 concrete implementation of view model. bind correctly, , determine parts of can abstract base class or interface if that's approach want take. it's not necessary, might make requirements binding easier "enforce"--for example, using base class or interface restrict (or else) changing name of property needed binding.

what put collection type? classes derive base class have different lists (person, car). base view model right place put property? want of derived classes implement seems so. , doesn't make sense have person , car extend base object.

if going use base class or interface vm , want collection part of it, can of type observablecollection<object>. items added need have property names match reference in xaml. can't have "personname" , "carname" properties if want 1 view; need use more general "itemname" (unless use datatemplates datatypes--this would useful). again, don't need each collection item inherit base type or implement common interface, unless (again) want enforcement @ compile time.


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -