Binding a collection to a list box - wpf

I have an object called TheObject.
Each 'TheObject' contains attribute A,B, and C.
I also have a collection of 'TheObject's
ObservalbeCollection TheCollection
I want to bind this collection to a ListView
so that it displays Coloum A, Column B, Column C across the screen.
So the list would resemble :
TheCollection[1].A TheCollection[1].B TheCollection[1].C
TheCollection[2].A TheCollection[2].B TheCollection[2].C
etc.
what is the simplest way to bind the listview???
Thank you very,much

This:
<ListView ItemsSource="{Binding MyCollection}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding A}"/>
<GridViewColumn DisplayMemberBinding="{Binding B}"/>
<GridViewColumn DisplayMemberBinding="{Binding C}"/>
</GridView>
</ListView.View>
</ListView>
[Binding basics]

Related

Populating a list view, from view model class in WPF

I am trying to add a list of model objects to my list view
This is my View Code which contains a list view as a Grid View
<ListView Grid.Row="8" Grid.ColumnSpan="3" Name="EmployeeDetailsListView">
<ListView.View>
<GridView>
<GridViewColumn Header="{x:Static resx:Resources.String1}" DisplayMemberBinding="{Binding EmployeeID}"></GridViewColumn>
<GridViewColumn Header="{x:Static resx:Resources.String2}" DisplayMemberBinding="{Binding FirstName}"></GridViewColumn>
<GridViewColumn Header="{x:Static resx:Resources.String3}" DisplayMemberBinding="{Binding LastName}"></GridViewColumn>
<GridViewColumn Header="{x:Static resx:Resources.String10}" DisplayMemberBinding="{Binding Gender}"></GridViewColumn>
<GridViewColumn Header="{x:Static resx:Resources.String11}" DisplayMemberBinding="{Binding Phone}"></GridViewColumn>
<GridViewColumn Header="{x:Static resx:Resources.String7}" DisplayMemberBinding="{Binding Phone}" ></GridViewColumn>
<GridViewColumn Header="{x:Static resx:Resources.String8}" DisplayMemberBinding="{Binding Phone}" ></GridViewColumn>
</GridView>
</ListView.View>
</ListView>
My View Model Contains the following code
List<EmployeeModel> eList = new List<EmployeeModel>();
eList.Add(EmployeeModel);
EmployeeDetailsListView.ItemsSource = eList;
I am trying to populate the List View from the View Model, but the ListView is not getting recognised in the ViewModel Class
I am getting the following error
"The name 'EmployeeDetailsListView' does not exist in the current context "
I want to know how to access ListView present in View.xaml from my ViewModel, so that I can populate the list view.
Thanks in Advance :)

Showing a CheckBox in a Cell of a ListView row isntead of a Boolean value in WPF

I have a ListView in a WPF Application that use Dynamic Binding. I wish to insert in the cell of a row a CheckBox instead than showing a boolean value for a specific row. It should apply to all the rows and should offcourse enable me to unset and set it (it will update the object it in the code) but i can do that last part myself. I just need to figure out how to put a checkbox there.
It is possible ? Any ideas ?
Here my XAML:
<ListView Margin="385,91,12,120"
Name="lstTransactions">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Description}"
Width="220">Description</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Path=MontantEstimation, StringFormat='{}{0:C}'}"
Width="100">Estimation</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Path=MontantReel, StringFormat='{}{0:C}'}"
Width="100">Réel</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Compte}"
Width="120">Compte</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding EstVerifie}"
Width="70">Verifié ?</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Thanks.
You gotta change the GridViewColumn.CellTemplate.
Check out the code snippet in this question WPF: Checkbox in a ListView/Gridview--How to Get ListItem in Checked/Unchecked Event?

How to show properties of a list in a list in a listview?

I am implementing an application in wpf , MVVM pattern.
I want to implement a listview with objects. These objects contains a list. On default this list contains only 1 object. But in the listview, I want to show that certain object. So the properties of the object in the list. This is my first problem.
But I also have to be able to have more objects in this list. My second problem is that i don't really know how to realise this? And i also have to show the properties of these objects in the listview.
Maybe a treeview? but i don't get how that i got to begin on this..
Someone with some ideas?
You can arbitrarily nest DataTemplates, e.g.
<ListView ItemsSource="{Binding Data}">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
<!-- Internal Manchines list gets its own ListView -->
<GridViewColumn Header="Machines">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ListView ItemsSource="{Binding Machines}">
<ListView.View>
<GridView>
<GridViewColumn Header="Model" DisplayMemberBinding="{Binding Model}"/>
<GridViewColumn Header="Manufacturer" DisplayMemberBinding="{Binding Manufacturer}"/>
</GridView>
</ListView.View>
</ListView>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Occupation" DisplayMemberBinding="{Binding Occupation}"/>
<GridViewColumn Header="Status" DisplayMemberBinding="{Binding Status}"/>
</GridView>
</ListView.View>
</ListView>
This can be improved in terms of alignment, e.g. you could set up a Grid in the DataTemplate with a shared size column to make all lists the same width (making the internal columns align might be a bit hard though)
Further you could define a style to collapse empty lists (which otherwise would show the header without items).

How to bind to ColumnCollection for a GridView using XAML

I have a collection of objects which I would like to represent columns in a GridView.
Currently I am binding individual columns in my GridView to items in the collection like this:
<ListView>
<ListView.View>
<GridView>
<GridViewColumn Header="{Binding Path=MyColumns[0].Title}"></GridViewColumn>
<GridViewColumn Header="{Binding Path=MyColumns[1].Title}"></GridViewColumn>
<GridViewColumn Header="{Binding Path=MyColumns[2].Title}"></GridViewColumn>
</GridView>
</ListView.View>
</ListView>
This works well, but what I really want to do is to bind to the collection itself (since the number of items in the collection might change at run-time). I would like to accomplish this in XAML. What I have in mind is something like this (note this does not work - the ColumnsSource attribute is make-believe):
<ListView>
<ListView.View>
<GridView ColumnsSource="{Binding Path=MyColumns}">
</GridView>
</ListView.View>
</ListView>
Can this be done? If so, how?
You can use an attached property to create a ColumnsSource property, see this question for an example.

Sorting a ListView by deriving GridViewColumnHeader

I am trying to implement a ListView with a GridView with sortable columns.
To sort the ListView I hook up the Click event for the GridViewColumnHeaders and adding SortDescriptors to the default view source (similar to what is done in MSDN).
Something like this:
<ListView ItemsSource="MY ITEMS SOURCE BINDING">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn DisplayMemberBinding="MY DISPLAYMEMBER BINDING">
<GridViewColumnHeader Content="My Header" Click="ColumnHeaderClicked"/>
This all works fine, but I would like to generalize it a bit. To do that I simply derived GridViewColumnHeader and wrote a click-handler. I know there are many sortable list view implementations out there typically deriving from ListView, but I was just wondering if this approach is possible.
Something like this:
<ListView ItemsSource="MY ITEMS SOURCE BINDING">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn DisplayMemberBinding="MY DISPLAYMEMBER BINDING">
<local:SortableGridViewColumnHeader Content="My Header"/>
For this to work I need to navigate from the SortableGridViewColumnHeader code to the containing ListView in order to set new SortDescriptors.
I tried navigating up the Parent ladder, but the GridViewColumnHeader is not a visual child of my ListView. Surely I could make a dependency property and bind it to the ListView, but there must be a way to navigate to it instead.
How would I do that in code? (I am not looking for answers on how to sort a WPF ListViews in general, I am wondering if it can be done this way).
EDIT
It turned out that what I needed was this parent searcher in the click-handler of my GridViewColumnHeader derivative.
DependencyObject parent = this;
do
{
parent = VisualTreeHelper.GetParent(parent);
if (parent == null) return;
} while (!(parent is ListView));
Now my sorting works like a charm.
There is a much easier way to do that, using an attached property. Check out this article for details.
<ListView ItemsSource="{Binding Persons}"
IsSynchronizedWithCurrentItem="True"
util:GridViewSort.AutoSort="True">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="Name"
DisplayMemberBinding="{Binding Name}"
util:GridViewSort.PropertyName="Name"/>
<GridViewColumn Header="First name"
DisplayMemberBinding="{Binding FirstName}"
util:GridViewSort.PropertyName="FirstName"/>
<GridViewColumn Header="Date of birth"
DisplayMemberBinding="{Binding DateOfBirth}"
util:GridViewSort.PropertyName="DateOfBirth"/>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>

Resources