Can someone please explain the concept of a binding container and binding context concepts in ADF ? How the above objects are instantiated during an ADF page lifecycle ? Does a binding container contain an instance of binding context or is it the other way around?
Binding Context is basically a map between data controls and page definition (contain bindings information) of pages in the application. Whenever an adf client or controller initiates an interaction with the business service, it(the interaction) is managed by the application through a single object. This object is the Binding Context.
Binding Container is used to instantiate page bindings. it contains bindings , executables and datacontrol mappings.
Binding Context contains Binding Container
Simple Example to clarify it further.. :
BindingContext bindingctx=BindingContext.getCurrent(); //get bindingcontext
BindingContainer binding=bindingctx.getCurrentBindingsEntry();//get container
DCBindingContainer bindingsImpl = (DCBindingContainer) binding; // get the Application Module(Data Control) using the references in Container.
DCIteratorBinding dciter = bindingsImpl.findIteratorBinding(“iterName”); // find the iterator using the Data Control
ViewObject vo=dciter.getViewObject();// get View Object instance for the VO whose iterator is "dciter"
Takle a look at these recorded trainings, they may help.
http://download.oracle.com/otn_hosted_doc/jdeveloper/11gdemos/insiderBinding1/BindingsPart1.html
In Brief, Data Bindings file contains the page Map, Page Definition references , References to Data Controls.
Data Controls file contains the data control definitions
Related
In my WPF app, I am using MVVM. I am reading from an XML file, deserialize it to an object model and keeping it in memory.
XML File->BusinessObjectModel(Model)->ViewModel
Whenever the viewmodel needs the model I will provide it from the memory.My problem is when I use the model elements in the views it is updating the model in memory(obviously!). I dont want to do that, I want the model updated only when the user clicks OK in the view(or dialog). How is it usually achieved? Should I only provide the viewmodel a clone of the model and not the original reference?
Editing a clone of the model object would solve the problem, as you suggested.
Another approach would be to have the property bindings use an UpdateSourceTrigger of Explicit. Upon clicking Save, you would programmaticcally call UpdateSource on each binding expression. This requires some extra code, which would belong in the View's code-behind since it is code that manipulates UI elements.
Also consider having a property on your VM for each property exposed in the View, where the backing field of the VM property is not the wrapped Model object's corresponding property. When the user clicks Save, you could then assign each property from the VM to the Model object. Naturally the controls in the View would be bound to the VM properties, not the Model properties. This is effectively like having a clone, without the extra baggage of supporting cloning in the Model layer.
I'm not suggesting that any of these options are better or worse. It all depends on the context in which they are used.
I'm trying to build a data entry form in wpf. To perform validation I apparently need to have an object attached in the datacontext of my grid. But how can I have one when I didn't create one yet?
How does it work?
For example, I have a screen with a datagrid. The datagrid contains users that were obtained from membership. Above the grid is a button: add user. When clicked a new window appears and the following can be entered: user name, password, email. To perform validation on the textboxes to see if they aren't empty. Now, it is my understanding that the way this works is by having an object attached to the window (datagrid datacontext). But how can I have it attached when it doesn't exist yet?
This is a case where MVVM design patterns are very useful.
Every WPF view has a corresponding view model object that the properties in the view are bound to. So your window with the data grid has a view model - its DataContext - and the view model has properties that are bound to properties in the view - e.g. the ItemsSource in the data grid is bound to a collection (see note 1).
The "add user" command (which is implemented as a RelayCommand in the window's view model) creates a new view (the new window) and its corresponding view model object (the new user), sets the view's DataContext to the view model, and calls ShowDialog to show the window. (See note 2.) If the user accepts the new object, ShowDialog returns true, and the logic in the command takes the view model object (which now contains whatever changes the user made) and uses the information in it to create a new model object and add it to the model. If the user cancels, ShowDialog returns false, and the command discards the view model object without creating a new model object.
Note 1: The collection here may be a collection of model objects, or it may be a collection of view model objects. It depends on whether or not you need anything that's not in the model for displaying the model objects in a data grid. It's common, in this kind of scenario, for the objects in the grid to be view models for the dialog - that is, the view model objects have properties implemented for both display in the grid and modification in the dialog window. On the other hand, if all the grid is doing is displaying data from the model, there may be no need for an intermediary object.
Note 2: Having the command create a WPF window violates a central MVVM design principle, which is that view models shouldn't create WPF objects. The reason for this principle is pretty simple: you can't build an automated unit test for this command, since it's just going to throw up a dialog and wait. There are all kinds of different approaches to this - see, for instance, this question, and Josh Smith's blog post on the Mediator pattern - and all of them involve delegating the creation and display of the actual dialog window to a separate service that can be mocked out for unit testing. If you don't want to choose one of those approaches up front, you can retrofit one into your application once you get this thing working.
The idea here is that you should attach an object which is slightly different from your business models. In your case it won't UserInfo (or whatever you have for users in grid). It will be some other class, more suitable for editing. In MVVM this class will be a ViewModel. This class will have some differences comparing to your regular user class, for example it may have some properties nullable (when you haven't set them yet). Also this class will handle validation. You should instantiate this class at the same time you're creating an editor window and put instance of this class into Window.DataContext.
Hmm, there is a lot in this question but I just created a screen with three data grids (I am using Telerik in this case) and under each datagrid is a button to add to the grid. No the window with the three datagrids has it's own view model. and each of the "pop up's" has it's own viewmodel, in this case all of these are user controls and I just create a new window and set window.content and call show dialog.
Communication is facilitated via "events" - not the standard events you are used to in .NET but in this case I am using Prism and it's CompositePresentationEvent class. When the user is done creating their new object they click add and I fire off this event with the "payload" being the object they created. The main window with the three grids listens for that event and has a method to handle it, in this case adds it to the ObservableCollection which is what I bind the grids to.
If I were you I would look into the various frameworks that are out there, Prism, MVVM light etc... Again, your question seemed rather broad, I tried to give an overview but I didn't go into detail, if you look into some sort of framework I think it will clear up a lot of these details for you.
When the users hit Add New, create a new blank copy of your object, and set the datacontext to that new object.
Set some kind of flag to identify that it is a New object. This can be the Id being NULL, 0, -1, etc or an ObjectState property set to New. That way all your validation rules apply, and once the user hits save you know to INSERT instead of UPDATE
There is a ViewModel that consists of some related object (nodes and lines( ,
How it can be possible to display (synchronize) these VM in View and keep object connections.
I use some DataTemplate to map model to view but each object would be synchronized (with powerful binding) to its related object but how can i link (and synchronize) this DataTemplate generated UI element together.
I describe problem from another viewpoint here:
Sunchronizing view model and view
To keep your view synchronized you should use bindings, your binding sources need to implement certain interfaces or be dependency properties though. For collections you need to implement INotifyCollectionChanged and for properties you'd use INotifyPropertyChanged, if you then change the source your view will change as well.
Next to implementing INotifyPropertyChanged and using ICollectionChanged (ObservableCollection) and binding to the views, you might consider implementing IEditableObject when you want to support the editing of the data.
This interface allows you to undo edit actions. Without implementing IEditableObject you would need to go back to the data source to reset the to the original values when canceling the modifications. The interface is also supported by the DataGrid.
You can synchronize the VM and View using Relaying Command Logic.
you can see a sample workout here
Binding the "WindowState" property of a window in WPF using MVVM
I'm a bit in doubt as to what is 'the right way'.
I have an application with concepts like visual studio, so I'll use that to explain:
I have a 'solution' view model and a model behind. The view model is displayed in a 'explorer'.
I can change between the situations where 'no solution is loaded' to 'a solution is loaded' and back.
And finally my question :-) :
Should I keep my view model object and let it reflect that I have a new 'solution object' loaded? Or should I create a new view model object and let the view bind to the new object?
Your Viewmodel contains the state of any data associated with the UI which is not further back in the Model.
One way I answer questions such as yours is by considering what behaviour I want in the UI and what needs binding to some state information. Or, to put it a different way, any time that I feel like writing some code that would cause UI elements to be shown or hidden, think about how that maps to a boolean variable.
So, take large chunks of the UI that only are visible when you have a Model loaded. These might have their visibility bound to a boolean property in the Viewmodel IsSolutionLoaded.
Maybe you want to disable some things if processing is occurring, you could have a property IsCompiling. I've used this approach with a property NotRunningthreadedProcessing as shown below, that let me disable controls when a synchronisation object existed in the Viewmodel.
CNTL_WhiteLevel.SetBinding(ProgressBar.IsEnabledProperty,
new Binding("NotRunningThreadedProcessing"));
// and the C++/CLI property concerned
property bool NotRunningThreadedProcessing {
bool get()
{
return mThreadedCommandSyncher == nullptr;
}
};
What is the arrengement of the ViewModel?
In General
View Model is the localized version of actualmodel and View is updated whenever there is a change i its viewModel through DataBiding.
in your case ,you have 2 states.
1- Your View is Loaded
2- Your View is not loaded
so should I create a new view model object and let the view bind to the new object?
in my thoughts ,YES
I have a WPF application consuming data using Entity Framework 4 and Self-Tracking Entities. In which I have a window with 2 controls in it, one that shows the "Details" portion of an object using a ContentControl and templates from a merged resource dictionary. Another with a ListBox of Groups the object in question belongs to and a ComboBox of available groups it could belong towith a button wired up via a command to the control to add/remove items from the bound collection of Groups based on the SelectedItem of the ComboBox. All this is bound together by DependencyPropertys.
In my Window I have DP's for the Object, EditedItem we are editing and a read only property with a List of Group of the groups it could belong to and bind that to my controls via XAML.
SO....
If I create a new instance of one of my entities, set it's properties like so: (Really this is the exact code)
Employee employee = Context.CreateObject<Employee>();
employee.Name = "Joe Nobody's Brother Steve";
employee.Active = true;
employee.Username = "snobody";
Group group = Context.CreateObject<Group>();
group.Name = "Losers";
group.DisplayName = "Spirit Squad";
employee.Groups.Add(group);
And set it as my Window's EditedItem it works FLAWLESSLY!
If I however fetch this exact same entity from my Database the Groups ListBox is empty.
Any ideas?
It turns out I had made a mistake else where:
I needed to call:
ObjectContext.LoadProperty(entity, navigationProperty);
on my navigation properties for them to get populated. I think this has something to do with my objects all being derived from a core object and the fact that I select them using OfType on the ObjectSet of the core object. Or it could be behavior but I would think I would have encountered it before now.
But hey I'll take working, and this is easy enough to integrate into my selection methods and properties.
Chalk this one up to ignorance of EF4.