I've got an ObservableCollection of POCOs (Plain old CLR Objects) that I want to represent in a tabbed idiom. Prefereably using the MVVM pattern, is there a way to bind the collection of TabItems to the count of my POCO collection?
So, in this case if there are 3 items in my collection, I'd like to see 3 TabItems. Each TabItem would contain the same controls in the same location, each control bound to properties of the appropriate object in the collection.
I'm just looking for an overview of the approach I might use or a link to an example. If you need more info, feel free to ask.
Thanks.
I'd probably create an ObservableColletion with your POCO items in it. You could then bind that ObservableCollection to any of the Silverlight Item Rendering controls. You'll have to modify the default rendering template to create your tabs...but using that method, your tabs will constantly be up to date with the items in the collection without having to put any code in the code behind file.
UPDATE
Here's a link to the Silverlight Forums where somebody built a TabControl using the ItemsControl with sample XAML code:
http://silverlight.net/forums/t/12271.aspx
...just scroll down a bit to see the sample.
One way to do this is to use a value converter (IValueConverter) to return your POCO wrapped in a TabItem. I posted an example here as part of a related question. There is also sample xaml binding and injection of the ViewModel as a parameter to the value converter.
/jhd
Related
I currently have a GridView populated with ItemsSource binding to an ObservableCollection.
It's showing the items as I intended but I'd like to manually add one more item at the end that looks and behaves differently. Is there a way I can do it without modifying the ObservableCollection the ItemsSource is bound to?
There are two approaches to this:
Using MVVM the original collection would be pulled up from the model layer and the additional item would be added in the view-model. It works as you can consider the additional item required only for the view, e.g. for example a list of poll options where you need to add an 'all of the above'.
Have two collections, separate, and then concatenate/join them together using an IValueConverter when you bind the ItemsSource. This approach is more tricky and only really useful when you need both collections separate and combined, e.g. a list of items in one collection and a list of item categories in another but also combined.
I have successfully used both approaches in a large widely-used WPF application.
Edit: Reading the comment on the question, if you did mean that you want a different visual or behaviour of the additional item you can achieve that using an Selector DataTemplateSelector and/or ItemContainerStyleSelector. I recently used a combination of option #2 and template selecting successfully.
Some options:
Use a CompositeCollection, you can define this is xaml. Not sure if it would solve the issue, you might need to play around with it a bit
Create a new collection behind the scenes that is populated with the items from your collection + the extra item.
Write a converter than creates a new collection (or returns an IEnumerable) with the new item added.
I guess all of these options have a common theme in that they are all a new collection. You basically can't bind directly to the original collection because it doesn't have the extra item, so you have to bind to something else in some form.
I'm writing an application using WPF, and I need to make something that looks like this (see image):
What control should I use for this?
If you don’t need the groupings (the “Hire as chef” and “Seek dinner invitation” headings), then DataGrid should get you close. You can bind its ItemsSource to your collection of items, and define custom columns bound to your items’ properties.
For example, you can use a DataGridTextColumn for “Occupation”, DataGridCheckBoxColumn for “Tells Jokes?”, and DataGridTemplateColumn for more complex properties which require a custom DataTemplate to visualize, such as the main “Person” column or “Cooking skill”.
Edi: It appears that the DataGrid does support grouping as well.
You should go for ListView
http://msdn.microsoft.com/en-us/library/ms754027(v=vs.90).aspx
I have created a silverlight 4 application and using visifire charting components.
I am using MVVM pattern and have created a view which contains:
<Charts:Chart x:Name="myChart">
The issue is that the number of data series of the chart will change.
I have got all of the logic in place now and is working if I don't use MVVM model and directly set the myChart's series property.
Now, I want to set the data series collection of the chart in my view model.
I have created a DataSeriesCollection property in view model.
If I was setting data series collection in my backend code (if I was not using MVVM), I would do simply:
this.myChart.Series = MyDataSeriesCollection;
where MyDataSeriesCollection is a collection of DataSeries
but I don't know how I need to set the binding of DataSeriesCollection in the xaml.
As at Sep 2010 it looks like the DataSeries property is not bindable:
See this forum post, aswered by Visifire: Databinding Series property?
It may that Visifire is a few steps behind the latest trends towards MVVM and binding everything.
I've just encountered the same problem as #InfoLearner and I believe that I know how I might address this issue so that I'll be able to dynamically add more DataSeries objects to my visifire chart using binding.
My idea is to create a UserControl that holds the Visifire chart within it (only the chart), then, I'll add some dependency properties with the stuff I want to be able to bind to (like DataSeriesCollection property, Styles etc.) and then I'll hook it up to my chart in the control's code behind. I believe it's the only viable solution until Visifire would be kind enough to provide their controls with properly bindable properties.
HTH.
I need to add a custom item to ListView control (like on the picture), is it possible ? And if it is, what's the best way to do it ?
I don't know that this is possible with Winforms. The list items in a System.Windows.Forms.ListView are System.Windows.Forms.ListViewItem objects contained in a strongly typed collection.
You could try to create a subclass of ListViewItem, but since that class inherits directly from System.Object and is not an actual windows form control, you might be borrowing trouble, as you would need to replicate all the functionality of the inheritance chain of an actual control.
Now, if you are not too far in to the project, you might consider looking at switching to WPF. The ListView in WPF uses controls as items, so you could easily create a usercontrol that you would use as your list items.
You might be able to find a control library with a control that gives you the functionality you want, but for the most part, the good libraries are commercial and can be prohibitively expensive for small shops and individual .
I did a quick google search for any library that offers this capability, but I could not find one that displayed custom controls.
Not sure if this is what you're after, but ListViewItems have a Tag property that can store custom data about each item.
http://msdn.microsoft.com/en-us/library/system.windows.forms.listviewitem.tag.aspx
How about creating a user control. In that case you can set your user controls at the top and listview below. Or are you trying to add these controls as items in the listview itself?
Say you have a xaml page that takes two inputs such as DATE and some TEXT value and displays the result on a DataGrid, its easy to have a class with with 3 properties of dataTypes DateTime, string and some Collection and implement the same in MVVM.
I have a xaml page with a DataGrid and few input controls(like textbox or datepicker) generated dynamically based on a certain criteria. I want to implement this in a MVVM pattern but not sure how to go about. Any pointers will be helpful.
In my experience, MVVM is not answer for all presentation scenarios, sometimes you just have to use codebehind or build custom control. I would suggest here building custom control that takes (binds with dependency property) object or list of objects, and generates controls (text boxes with input validation/verification, for ints, doubles, date picker, etc), and binds given object to them. In the end you will have one smart control that takes object and renders input fields, and binds to them. Your VM class would just expose object or list of objects.
It is always good to separate the view and the model; however this cannot always be done with simple data binding of simple types.
I would be looking at something like a custom control that could databind to a list of {name, value, spec} pairs when the “spec” is the logical type of control to use.
Thinking about it; that is like what a property grid does if you pass it a custom collection of property descriptors….
Devexpess has a “vertical grid” that may help, also look at 3rd party property grids.