WPF binding a class to listbox - wpf

I have used exprssion blend4 edit itemcontainerstyle and increased 3 textblocks in item. NOW, i want to bind the 3 textblocks to a class. The class has 3 different properties. For example,i has a student class,student has name ,age,and major.I want to bind whole school students to listbox and each textblocks display specific information.

You should read at least the MSDN Data Binding Overview. It provides the necessary basic knowledge about data binding in WPF.

Related

Any ideas how to implement ComboBox to represent hierarchical data?

Does anybody saw control like this somewhere?
I need to make such control to represent hierarchical data (it should be generic very likely, i.e. data binding, templates support).
Something like combination of ComboBox and MenuItem’s.
I think I will redefine the combobox itemtemplate with some hierarchicaldatatemplate along with popup class.
Just put ComboBoxes on a form and bind the ItemsSource to the top level collection.
Then bind the DataContext of the next ComboBox to the SelectedItem of the box on the left and bind its ItemSource to the collection of items.
You know how to bind to SelectedItem?
E.G.
Column1
Public String Name
Public List Column2s
So you bind the first combox to List with he displaymemberpath = name
Then on the second combobox you bind to Column1 selecteditem with items source path of Column2s
The trick is to build up the Lists within the Lists within the Lists
All right, I made it by custom control inherited from ComboBox, custom ComboBoxItem inherited from HeaderedItemsControl and using HierarchicalDataTemplate.

Databinding in the same Control on 2 different DataContext

In Silverlight I've a page with some controls and a listbox.
I'm using MVVM and the dataContext of the listbox is defined like this. In my model I have a property ProductCommand and this ProductCommand object contains a list of product named Products.
My listbox is in a grid with the datacontext defined as the ProductCommand property. and the databinding for the listbox is set to the Products (Binding="{Product, Mode=twoWay}").
In my model class I also have a selectedProduct property, and I want to bound it to the SelectedItem property of the listbox.
How can I do that?
I have had similar problems I found this blog article by Dan Wahlin on a Data Context Proxy very helpful.
Of course in Silverlight 5 ancestor binding will also provide you a means to solve this issue.

Where should I put list of UserControls and not break MVVM?

I have a tab control where each TabItem is a UserControl. I'd like to hold the UserControls in the TabControl's ItemsSource. Does ItemsSource list go in the Window's ViewModel? If so, I feel like it's breaking MVVM since the ViewModel would now have GUI controls within it. Or do I put this list in the codebehind of the window that holds the tab control?
Any suggestions would be great!
With tab controls, more often than not the individual tabs are created statically in XAML rather than at run time by data binding. However there is no reason why you shouldn't do this. If you have a collection of views, they should definitely be stored in a view.
Bear in mind that you could also bind the ItemsSource to a list of ViewModels objects and WPF will generate a view for you with the ItemTemplate, with the ViewModel object set as the DataContext. This collection of ViewModels should be stored in a view model, although at some point a view model will obviously have to be stored in a view.
This can most likely be done in a number of ways, all which are up for debate on how "MVVM-friendly" they are.
My setup looks like the following.
My Main window has a DataContext bound to a MainWindowViewModel which contains an property
public ObservableCollection<Workspace> WorkspaceCollection{get;set;}
MainWindow has a TabControl which ItemsSource is bound to WorkspaceCollection
Workspace are all viewmodels and are bound to different views/usercontrols via DataTemplates
You might have a look at the Write sample application of the WPF Application Framework (WAF). It has a TabControl where each TabItem is a UserControl and it does this by applying the MVVM pattern.
Here's what I've done.
I created an interface that all of my controls implement, IMyAppControl, which has some information such as Title, Description, other metadata.
My Main Window has an ObservableCollection that the tab ItemsSource binds to.

Unable to databind a Silverlight4 DataPager control with MVVM

I have a data driven Silverlight 4 business application with a fairly standard user interface. There's a side section that allows you to enter your search criteria, a standard Silverlight 4 datagrid control in another section that contains your search results (if any), and then a "details" section of the screen which shows the individual information of a single row of the grid when you click on it.
Just underneath my grid control, I have placed a Silverlight DataPager control. When my datagrid has databound search results, I want the DataPager control to be activated that lets you move forwards and backwards through the dataset.
I've got the whole user interface xaml page bound to a custom viewmodel class.
My viewmodel class has a public ObservableCollection property called "Applications". I then set the xaml of my datagrid control to bind to my Applications property:
{datagrid:DataGrid x:Name="grid1" ItemsSource="{Binding Applications}"}
The datagrid control binds to my viewmodel with no issues. However, I'm unable to find the correct xaml syntax to bind the DataPager control to point to my same viewmodel Applications property. So the end result is my DataPager control never activates and remains disabled.
I'm sure I'm missing something obvious, but hoping someone can send me a quick solution.
thanks in advance,
John
Turns out that the xaml for the
DataPager control needed to point to my datagrid control and the binding path to
ItemsSource:

Binding WPF TreeView with generic viewmodel

I have a usercontrol which contains a TreeView control. I am using MVVM pattern.
I want to reuse this user control in different windows, each time binding the usercontrol to a different datacontext.
<UserControl Name="UserControl1".......>
..............
<TreeView ItemSource={Binding ...}...>
<HierarchicalDataTemplate...........\>
</TreeView>
..............
</UserControl>
In window 1, I want to bind a List<ObjectA> to the TreeView.
In Window 2, I want to bind a List<ObjectB> to the TreeView.
Is it possible to write a generic ViewModel for this usercontrol, so that I can bind different Types of data to the TreeView??
In case my question is not understood, please do let me know.
If I am reading this correctly, you have a UserControl which you wish to reuse, setting its DataContext to be different ViewModel's in different parts of your application...
that being so, yes you can certainly specify Lists of different types as an ItemsSource for your TreeView, but:
The list property must be consistently named within each ViewModel
You will need to describe a DataTemplate (or HierarchicalDataTemplate) for each type you expect to pass into the TreeView within your control's xaml
You may find that binding to an ObservableCollection<T> brings greater reward than a List<T> if you wish to add/remove items to/from the collection and hope to see those changes reflected in the UI
Hope this helps :)

Resources