UserControl instead of DataTemplates - is there a way for big collections? - windows-phone-7

There are two ways. May be both are stupid...
I have to display some collections of items.
First one.
I use DataTemplate for ListBoxItem.
Just set itemSource = myCollection;
That's all. Simple scheme.
Second one.
Each item in my collection has property view. It's a UserControl. That define how item renders.
Create DataTemplate with ContentPresenter only.
Binding Content property to a view.
Just set itemSource = myCollection;
That's all. More complex. But works too.
Has second one right to live? My doubt is that I have to create instance of UserControl for every item in my collection?
Is not it too expensive for collection with over than 500 items?
Thanks.

I don't believe there is much difference, with the DataTemplate approach the framework will create an instance of the DataTemplate for each item in the collection. In the second approach an instance of the user control will be created for each item, there may be a few more controls but only a few per item.
One reason the second approach could be preferable is that you can have logic around which content is bound. This could mean different user controls for each item in the list. Caliburn Micro lets you use this approach very naturally.

Related

IsEnabled Binding performance concerns?

I am dynamically loading around 1000+ items which are translated to buttons in xaml.
The view model has an IsEnabled property that is binded to the IsEnabled property of the button in the View.
My question is, won't this affect performance? I only need the property on some of the items but all of them use the same viewmodel and it is quite hard to introduce a new view model just for that purpose.
I am using Windows Store 8.1.
Thank You!
1k bool bindings should not do any harm. I've seen applications with 5-10k bindings beeing populated at once, they can freeze a little bit if done in sync. Your item/list in viewmodel loading should be done async to avoid UI freeze if the collection of those items is bound instantly with all the items already there, that is why observable collection is a fundamental thing to bind to.

GWT Watermarked (AKA placeholder) ListBox

I want to have in my gwt application a ListBox with a watermark.
I already created TextBox and a DateBox with a watermark by extending those classes and the property placeholder of the DOM.
However I could not such property for the ListBox. There isn't any, right? (how can I tell? except for trying.)
Assuming that there is no such property I would like to implement such a class by extending ListBox.
I am not sure how such a ListBox would behave.
could you please help me define the behaviour of such ListBox or maybe you know a site that uses one so I could play with it? Or just point me to a code example.
Thanks
I would think it would be a non editable list item as the placeholder. You could have the setPlaceholder set the text on the list item. The placeholder list item would only be visible when the list was empty. You couldn't select the list item, and all methods to access list items would never see it.

Setting DataContext in UserControl from ViewModel

I have a View that contains a Listbox. I'll call it CityListingPage.xaml. This list page has a CityListingViewModel, that is binded with the View like somewhat like this:
DataContext="{Binding CityListing, Source={StaticResource Locator}}"
This works nicely. Now I what to change my page to a Pivot Control, where the Pivot Items, would be instances of CityListingViewModel, but obviously with different constructor data (ie. Country)
I extracted the ListBox into a UserControl. Now I'm struggling how to make this work so that for each list I get a new instance of the CityListingViewModel.
I tried creating in the ViewModelLocator a collection of CityListingViewModels but how do I pass the a CityListingViewModel instance to the UserControls DataContext?
Perhaps there is a different, better way of doing this?
Without seeing your code, I'm going to do a little guessing, but I think you can do it directly via data binding. Since each pivot item is getting an instance of CityListingViewModel, you can just pass that binding along to the UserControl:
If you post a little more code showing what you're trying to do, we might be able to be of more help.
The following are two answers for using a collection to create panorama pages. But I am quite sure that the approach can be adapted to pivot pages:
Dynamically add eventtocommand actions to a listbox
Static and dynamic panorama items in a panorama wp7 mvvm
The second post should be more relevant.
If you are thinking of partitioning the same data over multiple views on a pivot page then I would suggest NOT using several view models, especially if it is the same datasource you are using for all the data.
Simply have a parameter which each view would bind to and use Linq to control what data is visible to that parameter.
So you will have the variable which will contain all the data to be displayed and one parameter per view querying that data.

Unify ViewModel through-out UserControl

I'd like to know how to set a single ViewModel throughout a single UserControl. I'm using an Items container inside a user control (bound to ItemsSource) and it doesn't seem to update with the code-behind replacement of a DataContext (to a code-behind instantiated ViewModel)
What i'm trying to do is change the DataBound foreground color of every text Item in the userControl, and the items inside an Itemscontrol dont seem to change. forcing a datacontext change removed the collection items from display.
I think I'm conceptually Misunderstood here. could anyone help?
I could fix the problem by using Storyboards to change color but the problem was still the same. The eventual solution was to access the resources inside the templates, which is possible by browsing the Visual Tree as shown in this tutorial
http://windowsphonegeek.com/tips/how-to-access-a-control-placed-inside-listbox-itemtemplate-in-wp7
The peculiar thing though, was that I needed to look for my object inside the initial object returned, as it seems to return the System generated one, which you don't see yourself. Looking for the answer inside this one helped. Also, pushing them into a list of items for easy access later could be a general idea, but MS needs to fix the way of doing this pronto.
I would recommend binding the ItemsSource to a property in your ViewModel.

How to bind test data to a SketchFlow ComboBox?

I haven't been able to successfully bind some test data to a SketchFlow ComboBox. I added a sample data source and created a collection with some basic string values. There are a number of tutorials on how to bind to a ListBox in SketchFlow and that works fine, just not finding anything for a ComboBox.
One thing that caught me out a few times is that you need to have your data in "list mode" before you drag it on to the control (combo box or list box).
It seems to work as I expected, so perhaps I'm not understanding your issue correctly, but I'll give it a shot! I created a sample DataSource with a couple of strings in it, added a ComboBox to the layout root and then just dragged the Collection from the DataSource and dropped it on the ComboBox.
Keep in mind that if you drag the DataSource itself, you'll only be setting the DataContext property of the ComboBox and that's not enough to get your items to display. You also need the ItemsSource bound to the Collection of the DataSource and an ItemTemplate or a DisplayMemberPath to tell the ComboBox how to display your items.
Additionally, if you use the drag and drop method of binding, it will use the ItemTemplate approach for you, which may or may not be what you want as it will generally create a StackPanel and display all fields from the row in your Collection per item in the ComboBox. You can remove the ItemTemplate and set DisplayMemberPath to whichever field you wish to have displayed from your data source's collection.

Resources