Model Binding Complex Types - c#

What I am trying to do is on one page have a FormView strongly typed with a nested ListView that also strongly typed. The model hierarchy is ClassA(FormView) with a property of List<ClassB>. This is for insert functionality.
I've attempted this many different was... for loop, asp:repeater, listview, when i click insert on the form view i can't figure out how to get to the collection of ClassB models. do i need multiple forms?
the interesting part is since these objects don't exists there could be any number of ClassB models. I sticking with 5 for now but the user will define how many of ClassB they need before seeing this page.
Any suggestions?

Related

Dynamically creating ViewModel for View with many UserControls

I am working on a project in C# with many complex UserControls. Things like tanks, valves, pumps, gauges, etc. A view can have anywhere from a couple to 40 or 50 controls on it. I will ultimately have hundreds of views made up of these Controls. (I am generating them programmatically from old versions of the screens). The problem I have run into is creating a ViewModel for my views. I can not make a static ViewModel for every page. I need to dynamically create a ViewModel based on the controls contained in the view.
I am able to find what controls are on a view and create a model for each but I dont know how to create a ViewModel which lets me bind to those models without creating a ViewModel for each individual control.
Any help or advice would be appreciated, Thanks!
I think I was able to solve the problem. My ViewModel has a Dictionary with a unique Identifier as the key and the Model object as the value.
I go through the controls on a view, create a model and add it to the VM dictionary. My Binding is to the dictionary as such:
Vm has a dictionary called Models:
Dictionary<string, Model> Models;
View
{Binding Models[KEY].Property}

WPF - NotMapped annotation in EF6 not storing / saving property value(s)

When I try to insert or update data in my WPF usercontrol datagrid, the data is not being saved to the corresponding property. This is being caused by (at least so I believe) my bound property having the [NotMapped] attribute, all the other properties without the annotation are working correctly.
The data which need's to be updated is inside a DataGrid component which is bound to an ObservableCollection with the appropriate model. Inside the model there are several properties with the [NotMapped] annotation, these properties should be excluded in the creation of the table (model) in my database, though I do need them for binding input, hence the use of the [NotMapped] annotation.
Since the data is bound to a ObservableCollection I can't add the [NotMapped] properties to the usercontrol directly (so they wont be a part of the model).
Below an example:
Part of the XAML
In the image below we can see 1 property (pBreedte) which is 1 of the NotMapped properties, as well as the itemsource of the datagrid:
UserControl Code behind (part of it)
Part of the model which is used in the ObservableCollection
The model is being used for EF6 (code first).
Is there any way that the NotMapped property values can be stored / saved?
The easiest would be to just include the NotMapped properties in the database (so removing the annotation completely) but I am trying to avoid this.
More background information
The NotMapped values are added because they function as a placeholder property. Initially I had several decimal properties bound to the datagrid directly, but textboxes can't handle decimal values very well (WPF validation rule preventing decimal entry in textbox?). So I created a string placeholder for those decimal properties, when the form is being saved the decimal properties are being set to their string placeholder counterparts. This way the user can add decimal places without having to use a delay, value converter or anything.
If you don't need that information in your database, then don't store it - meaning your approach is good.
What I think here what might be the problem is that you are using your entity/database model as UI model.
I would suggest that you try to introduce a different model for the UI controls and user input. The models might seem to be duplicate at the beginning but while you are working on your application they will start to differ, but still describing the same items just form different perspectives.
Example:
Entity model has a class CarEntity. It is a pure POCO class, having only the needed properties that will contain the data in the corresponding table.
Ui model has a class CarUi. It has the same properties as the CarEntity. They are loaded and mapped from the database (from the CarEntity) shown to the user. If the user changes something, the diff values are mapped from the CarUi to the CarEntity and then stored to the DB.
With this separation of models approach, you should not face the issue where one constraint (mark column not to be stored in a table) influences other functionality.
Hope this helps,
Cheers and happy coding!

MVVM: custom class for grid objects that is only accessible by code behind

I'm writing a C# UI and trying to follow the MVVM pattern. My design consists of a grid representing a join of two objects in the database, some text fields, and some buttons.
I've been successful binding the data context of the xaml to my view model code, and my code-behind (.xaml.cs) is very minimal. I've done this by adding a class called GridObjs to hold the grid objects (i.e. objects with properties specific to the UI that aren't members of the model layer objects). Unfortunately, however, this approach requires that I expose my GridObjs class publicly in order for it to be used by the .xaml.cs.
Is there a solution that allows me to hide GridObjs but also allows it to be accessible by the .xaml.cs?

Making a grid of objects

I'm trying to make something like a quiz application where 3 questions will be brought up on screen at a time, allowing the user the check a radio button containing "Yes" or "No", and have an answer come up appropriately to his response. The questions will continually come from a database I'm using.
After a few attempts I've figured that using x:Name property is not a great solution, since it doesn't allow me to use a loop in order to change the questions and answers. Is there any other way to make a grid with the same types of objects in each cell, being able to access each object inside each cell of the grid in the code-behind?
Here is list of steps you need to implement,
Need to create QuestionModel, contains question properties, make sure your model inherits INotifyPropertyChanged.
Need to create ViewModel, which contains data objects, public/dependency properties
Need to bind/set data objects/properties on viewmodel constructor
Need to set your ViewModel as a DataContext of your View(.xaml) (You can create this on zammel directly and codebehind as well
Need to bind your UI objects like Question/answers/yes-no with viewmodel properties accordingly
WPF/Silverlight has their own fundamentals like Data Binding, Resources, Compiler, Dependency Properties. Above steps comprises MVVM design pattern. During each steps, please google for specific stuff.

Dynamically Adding Different Views Using MVP

First off let me say this is my first attempt into trying MVP. I am going for the Passive View approach as I want to completely decouple the Views from the Model.
I have read up on MVP by reading the following articles among others
Derek Greer
http://aspiringcraftsman.com/2007/08/25/interactive-application-architecture/
Todd Snyder
http://blogs.infragistics.com/blogs/todd_snyder/archive/2007/10/17/mvc-or-mvp-pattern-whats-the-difference.aspx
My application is going to have 4 Views that are custom controls in which each implement a different interface to interact with the Presenter. Now this is not a problem for 3 of the views as the controls are static and never change.
My problem comes along where on the 4th View I need to change the control/View that is displayed based on events triggered from View 1.
So for example lets say View 1 is a list of people who can be from either an employee or a customer. Now depending on who is selected you can modify different parameters depending on the type of person selected. The problem is that View 4 needs to dynamically change the editing control based on what is selected. Keep in mind not only the properties will be different but how they are edited as well. Also each editing control is going to fire different events for property changes.
So how does my Presenter know how to manage interaction with the Model, when depending on whether an employee or customer is selected there is a different editor that implements a different View interface?
I hope that was clear. Any help is greatly appreciated. I have searched all over and all examples show a concrete relationship between the View and Presenter whereas mine needs to be extremely flexible.
My goal here is to keep the editing controls on the 4th View so flexible that I could for example add editing controls/Views for aliens or dogs or whatever else I might add to View1 to be selected.
Thanks
You can create a top-level presenter that listens for selection events and changes the editing control by instantiating different MVP triads based on what is selected. Typically in MVP your presenters manage all the construction/dependencies.
I personally don't like having a 'Master Presenter' per se. I do build composite presenters: e.g. View can contain one of several views, so I build a Presenter for the master view, which sends commands (messages, events, whatever) to a specific child presenter (I don't expose any View outside it's owning Presenter).
Don't over-complicate things ...use a different View for each responsibility. Views are cheap.

Categories

Resources