Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple input forms with same xaml file and different DataContexts

I am developing a WinRT app using XAML and MVVM Light. This app is meant to make it easier to do data collection while the users are out in the field. I have a section of my app where users will need to enter in a bunch of information about several different items. These items are defined as classes that inherit from a GenericAsset class. The GenericAsset has fields such as this:

public class GenericAsset
{
    public string AssetId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }
}

And the sub classes look something like this:

public class SubAsset1 : GenericAsset
{
    public string RecordNumber { get; set; }
    public int SizeDiameter { get; set; }
    public string MaterialType { get; set; }
}

public class SubAsset2 : GenericAsset
{
    public string Type { get; set; }
    public int Size { get; set; }
    public string PlanRef { get; set; }
    public string InteriorMaterial { get; set; }
}

Currently I have 15 SubAssets (and will have many more in the future) and I am looking for a way to create one data entry view/viewmodel (if possible) so that I don't have to create a separate view for each asset. Also, if I can get the generic view/viewmodel working, how would I go about loading in the custom data entry controls (the inputs specific to each sub asset) while maintaining the proper two-way data binding to the appropriate sub asset?

like image 277
Malzarus Avatar asked Nov 11 '22 06:11

Malzarus


1 Answers

What you're looking for is a DataTemplateSelector. Create a different DataTemplate for each SubAsset. Then, display the Asset via a ContentControl (or ListView in the case of multiple of them). Both of them have a slot for a DataTemplateSelector (ContentTemplateSelector and ItemTemplateSelector, respectively). If there are parts that are similar between them, you can actually compose one DataTemplate with another by using an inner ContentControl pointing to the target DataTemplate (that you wish to compose).

In order to add your DataTemplates from different ResourceDictionaries, when you create your DataTemplateSelector, create a property for each DataTemplate that you wish to have.

Your selector may look something like this:

public class AssetDataTemplateSelector : DataTemplateSelector
{
    public DataTemplate SubAsset1DataTemplate { get; set; }
    public DataTemplate SubAsset2DataTemplate { get; set; }
    ...

    // Data Template Selection Code
    ...
}

Then in a ResourceDictionary (such as Generic.xaml), when you declare your AssetDataTemplateSelector, just refer to all of the other DataTemplates as StaticResources.

<!-- This assumes that AssetDataTemplateSelector has been declared in a namespace 
     defined in the root of the ResourceDictionary as 'converters'. -->
<!-- It also assumes that you have created DataTemplates with the names 
     SubAssetXDataTemplate either in the same or other ResourceDictionaries 
     which are accessible from this one. -->
<converters:AssetDataTemplateSelector x:Key="AssetDataTemplateSelector"
                                      SubAsset1DataTemplate="{StaticResource SubAsset1DataTemplate}"
                                      SubAsset2DataTemplate="{StaticResource SubAsset2DataTemplate}"
                                      ...
                                      />
like image 185
Nate Diamond Avatar answered Nov 14 '22 23:11

Nate Diamond