MVVM combobox binding issue - silverlight-4.0

I have a project with a number of comboboxes, all binding correctly. Since installing SL5 I started experiencing all sorts of binding issues as well as the debugger being crippled. The problem is that when you switch from SL4 to SL5 and then back to SL4, not all the references are reset. Long story sort, I had to reinstall vs2010 to fix those issues. Now I am testing the dev environment and although I can get a textbox to bind from the VM, I cannot get a combobox to bind.
What I would like to know is what is the absolute minimum requirements for a combobox to bind. My ViewModel's property SET is getting its data.I have the comboboxes ItemSource set and the DisplayMemberPath set.I have tried using the ItemTemplate syntax but nothing works. The RaisePropertyChanged is being fired. Could this be a UI threading issue? The only difference between this combobox and the others is that there is a call to the database that collects values that are then used in a second call to the database. Those results are then passed to the property in the VM which raises the RaisePropertychanged event, but the binding doesnt render. I have mocked an entity in the class constructor and the binding works, but when I load the entities from the database it doesnt work.

I found the issue. This might be helpful to others. I was instantiating a ViewModels ObservableCollecion in the Completed event of the method and then looping through the collection from the database and addng it to the collection. Binding was not happening. I moved the instantiation of the ObservableCollection to the ViewModels constructor instead and the Binding started working. Still a bit puzzled as to why the newing up of the ObservableCollection has to be in the ViewModels constructor and not in the Completed event of the method fetching the data.

Related

Aurelia validation on dynamically created form

I was able to get Aurelia-validation working on a dynamically created form when using the compose element, but I've switched to custom elements for better encapsulation and reuse of the custom controls. However, now my validation seems to be half-broken.
https://gist.run/?id=6e97538c3888cae0f6134faed9d67362
Issue 1: The ValidateBindingBehavior is not doing anything. I suspect it's not finding the controller or matching the rules since the property name is not easily visible in the binding (due to dynamic controls).
Issue 2: For some reason validate() on submit actually only shows the first error instead of all of them. That indicates a problem but I don't know what.
Can anyone get the attached GistRun to work properly?

Side Effects of Binding Errors?

I have a UserControl that I'm using as a template for two classes. This works fine because they both have the same property names. However, one of them has a few properties that the other one doesn't. This leads to binding errors in my Output window when binding to the class without the extra properties. It still works fine and the user can't even tell there was an error, but my question is: Are there any undesirable side effects caused by this?
(For the record, I plan to use x:DeferLoadStrategy to just not render the fields if not necessary. I'm merely curious about this.)

Changing a oneway bound TextBlock value from code behind.

I have a TextBlock and its Text property is bound to a ViewModel property. The binding is Oneway.
When I change the Text property of the Control from the xaml.cs the binding gets broken. If the binding is TwoWay I don't have this problem but the source property is updated too. Is it possible to have OneWay binding and change the target property value without braking the binding?
I suggest a workaround, like setting the Binding to TwoWay and ignore the update in the property. Something like this:
private string textValue;
public string TextValue
{
get { return textValue; }
set
{
:
}
}
Now the Property can no longer be set by the view.
Although no code is provided, this scenario typically occurs when you have bound a control to a view model and at a later stage your logic tries to update the value in the control programmatically.
You should not try to do this, that is define multiple sources of the value for a control. If you bind the control to a property on the view model, then to update the value in the control you should update the field in the view model.
If you were to set the value of a bound control programmatically at runtime so that it no longer matched the bound object value, when some other event causes the control binding to be re-evaluated, the value that you have provided programmatically would be overwritten again, you could easily end up with a scenario where the value you provided programmatically is never visible to the user.
In this scenario you should either:
Add a new property to the view model, bind this value to the control, then your program logic can set this value equal to the original property when the data is loaded, and updated when you need to
Not use bindings at all, always write to the control programatically that way you tightly control when the value is updated.
There is a workaround to this if you absolutely must have one. if you are using compiled bindings (x:Bind), then because the bindings are compiled it is possible to call SetValue on the bound dependency property at runtime and the previously compiled bindings will still be evaluated. However I advise against exploiting this because it makes your code a lot harder to follow and debug, when bindings are used, we tend not to look for code that directly addresses and sets control values.

How to restore an object in Silverlight 5

I have created an application by using Silverlight 5. There are many two-way databind in it. I want to retore a change in UI by restoring the object behind. For example:
I have an object behind which contains properties double Left, Right, I inherited it from INotifyPropertyChanged. In UI, I created tow button (ButtonLeft and ButtonRight) and their parent a Canvas Control, ButtonLeft bind Left property in two-way mode and ButtonRight bind Right property in two-way mode. It works very well, if I drag ButtonLeft, then the Left property will be updated with the new Left Value, same for ButtonRight.
Is there any way to restore the old value after the change of UI. Actually, the relation of class in my application is very complicated, I have already knew that retore the value of property in object will restore the UI, so is there any way (more general and loose-coupling) to restore all the objects that need to be restored.
I have thought of creating an interface IRestorable (inside Store(), Restore()) for objects that need to save and restore state. An attribute Restorable for property which need to be restored. For me, the use of AOP could be a good idea, each time when an instance of IRestorable is created, AOP will call a method for saving the current state (properties which is marked an [Restorable]) of object. I don't know if I was on the right way. If not, any advice will be appreciated.
Use of memeno design pattern.
http://www.codeproject.com/Articles/18025/Generic-Memento-Pattern-for-Undo-Redo-in-C

RadTreeView cannot refresh to show changes in hierarchical data source

I have a rather serious problem trying to get a Telerik radTreeView to show changes in the underlying database and therefore datasource. My scenario is as follows.
In the Load event hander, the radTreeView is initialized as in the following example code:
radCommentTreeView1.DataSource = Nothing
// Initialize custom data object from custom hierarchical data source class
cdsPubCommentDataSource = New CommentDataSource(1)
radCommentTreeView1.DataSource = cdsPubCommentDataSource
// Configure radTreeView from datasource fields
radCommentTreeView1.DataFieldID = "ID"
radCommentTreeView1.DataFieldParentID = "ParentID"
radCommentTreeView1.DataTextField = "Content"
// Bind radTreeView to datasource
radCommentTreeView1.DataBind()
The page loads with radTreeView rendered correctly
User attempts to add or delete a node by clicking a button with a server-side handler ....
... so that the postback cycle begins in the server-side code, with the Load handler from (2) above running again, to rebind the radTreeView
... only after which the server-side button handler is run, successfully running an SQL command through another BLL business object (i.e., the SQL database is successfully modified ) ....
However, when the page renders after the postback, the radTreeView still reflects the original node hierarchy from (4) above, before the node was deleted in (5).
Could someone indicate how this process ought to be achieved within the cycle of a single postback - if I call DataBind on the radTreeView at the end of the server-side button handler, this does seem to pick up the database changes, but unfortunately it destroys the radTreeView's formatting with the node template, and as a result, no node databound content is rendered in any of the nodes. Obviously, the node template needs to be instantiated at the start of the page life-cycle, and if, as in this case, databinding late in the cycle disrupts the template, I cannot tell the page to 'go back' and instantiate the template again. But calling DataBind at the end of the handler would seem to me to be the intuitively correct way to approach the issue, it's just that the resulting loss of the node template prevents it from working.
I am aware of the idea of using the INotifyPropertyChanged interface for the datasource object, and already have the event in question being successfully fired up from the DataSource object, namely a PropertyChanged event that is fired at (4) when the radTreeView first rebinds on the postback, but I am not sure how a PropertyChanged event handler can help in the scenario I describe - the PropertyChanged event handler in the page is still invoked before the button handler, and so the database has not been changed at that stage. Is the PropertyChanged event the correct way forward, or is there another approach better suited for refreshing the radTreeView within a single postback cycle?
Thanks if someone can point me in the right direction with this problem.
Regards