WP7 Binding on collapsed/non visible controls - data-binding

I have a Listbox in a PivotItem, as well as a ListBox in a Grid which is normally collapsed. Both ListBoxes have their ItemSource and SelectedItem properties bound to a view model. I put a breakpoint on the getters and setters of the ViewModel properties and noticed that they get called even if the bound controls are not visible or collapsed. Is it possible to have the binding only active when the bound control can be interacted with?

Only if you want to write code to unbind and rebind it. Not possible with pure markup.

Related

ObjectDataSource and FormView: how to add UI edition behaviour from business layer

I'm using an ObjectDataSource and a FormView.
In the FormView I have a set of controls. When the FormView is in edition, I have in particular a ComboBox and a TextBox which are related as follows:
when the ComboBox takes some special values, the TextBox must be read only or not.
For the moment I get that behaviour as follows: the ComboBox triggers a postback when its selected item is changed and in the 'OnPreRender' of my page I get the value of the ComboBox and update the Readonly property of the TextBox accordingly.
What I don't like with this method is that I don't use my object model which is consumed by the ObjectDataSource. The problem is simply that when the FormView is in edition there does not seem to be a way to get the instance of the object which is being edited. The 'DataItem' is null and I haven't found any way to automatically build a new object from the values in the controls to pass it to my business layer. Of course I could do the whole job myself by getting explicitly all the values in the controls, but that's not nice.
Has anybody ever met such a scenario ? Any ideas on how to get a clean design ?
How about using DataItemIndex or DataKey? I would probably use DataKey and then use the business method to look up for the object for the key.

How to hide a databound control

Depending on the case, I'd like to hide a databound control in a page. But no matter what I try, it seems like the control will try biding no matter what. I've tried setting Visible="false", but it would still try to bind. I've tried putting the control into a placeholder and then hide the placeholder, it will try to bind anyway. I've also tried putting it into a MultiView, same thing. You would think that in a wizard interface using a MultiView you would not want the controls in the next steps of the wizard to bind, but no. It binds anyway...
The only way I've found is to unset and set the DataSourceID property which seems to prevent binding.
Is this really the only option?
Thank you.
I think that you are on a good path. Do not set the data source id. When it comes time to display the data (which I presume is triggered by a user click), explicitly databind your control.
when you set the datasourceid on your control, then the asp.net framework will automatically data bind when the page's OnDataBind event occurs.
Could it be that you're using a datasource control to bind to? The DataSourceID property being set indicates so. If you don't want it to bind to such a control you can opt to take out the DataSourceID property in markup and explicitely set it in codebehind only when you need it.

winforms synchronise combobox values

I am trying to synchronise the selectedvalue of a combobox when navigating through records. Setting the combobox.selectedvalue doesn't work. I have tried refreshing the combobox but that doesn't work either. The combobox is databound.
If your ComboBox is data-bound (i.e. you've properly set its DataSource property), then you also need to make sure its DisplayMember and ValueMember properties are set to the correct fields.

ASP.NET Accordion

So I'm amending someone else's code, and they've used a data-bound Accordion control. I need to disable the Accordion functionality so that the top item is expanded and the rest are just minimized and kept that way.
Also is there any way to put a class on the final item?
Thanks a lot in advance.
The easiest thing might be to replace the Accordion control with a Repeater that is styled to look like the current Accordion.
But, to attempt to answer your question(s):
Create a OnItemCommand event handler that just sets the SelectedIndex to 0. That way the user can't open any other items in the Accordion.
As far as the style of the final item, you'll have to create a OnItemDataBound event handler that knows how may items are in your collection, and then count the items that it has bound. When the method is binding an item with the index that matches the size of the collection you just set the item's CssClass to be the class for the final item. (This same technique should also work if you were to replace the Accordion with another control.)
Check the first answer for the question 'How do I bind an ASP.net ajax AccordionPane to an XMLDatasource?' for a example of the ItemDataBound event handler.

Persisting Checkbox State Across Postbacks

I have a web form that binds a DataGrid to a, normally, different data source on each postback. I have a static CheckBox column that is always present to the left of the autogenerated columns. I achieve a TabControl effect with a horizontal Menu control above the grid, with each menu item being a tab that contains a different grid.
Now I would like to persist the state of these checkboxes for a particular 'tab', when another tab is selected. I would welcome any imaginative solution for doing this without using session variables.
I think the best bet for this is to have a different gridview for each of your "tabs". Use the MultiView control with a View control for each tab, and a gridview in each View. In the click event of your menu change to the correct view. Only bind each gridview once, and then your checkboxes will persist.

Resources