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 :)
Related
I have a context class with 3 DbSets and one-to-many relationships:
(1)Locations - University(many)
(1)University - Rating(many)
And I need to display the result of query to all Ratings in my listview in wpf.
But listView doesn't display fields from University and Location.
Query example:
using (var c = new Context())
{
ratingListView.ItemsSource = (from w in c.Ratings
where w.Year.Value == 2012
select w).ToList();
}
ListView bindings:
<GridViewColumn Header="Rank" Width="Auto" DisplayMemberBinding="{Binding WorldRank}"/>
<GridViewColumn Header="Institution" Width="120" DisplayMemberBinding="{Binding Unviversity}"/>
<GridViewColumn Header="Location" Width="60" DisplayMemberBinding="{Binding Location }"/>
<GridViewColumn Header="National
Rank" Width="60" DisplayMemberBinding="{Binding NationalRank }"/>
<GridViewColumn Header=" Quality of
Education" Width="70" DisplayMemberBinding="{Binding QualityOfEducation}"/>
<GridViewColumn Header=" Alumni of
Employment" Width="70" DisplayMemberBinding="{Binding AlumniEmployment}"/>
<GridViewColumn Header=" Quality of
Faculty" Width="70" DisplayMemberBinding="{Binding QualityOfFaculty}"/>
<GridViewColumn Header="Publications" Width="70" DisplayMemberBinding="{Binding Publications }"/>
<GridViewColumn Header="Citations" Width="60" DisplayMemberBinding="{Binding Citations }"/>
<GridViewColumn Header=" Broad
Impact" Width="50" DisplayMemberBinding="{Binding BroadImpact}"/>
<GridViewColumn Header="Patents" Width="60" DisplayMemberBinding="{Binding Patents }"/>
<GridViewColumn Header="Score" Width="60" DisplayMemberBinding="{Binding Score }"/>
You only selected fields from the Ratings table, so how do you expect to be able to display fields from the University and Locations!
Option 1:
Bind University and Location columns using navigation objects, such as University.UniversityName and University.Location.LocationName (I made up the member names as you have not supplied the whole structure of you EF).
Option 2:
Include the University and Location in the linq query.
using (var c = new Context())
{
ratingListView.ItemsSource = (from w in c.Ratings
join u in c.Universities
on w.UniversityID equals u.ID
join loc in c.Locations
on u.LocationID equals loc.ID
where w.Year.Value == 2012
select new {loc.Location, u.University, w.field1, w.field2}).ToList();
}
I am using a thread for reading the file and make a array of objects from the data read.A seperate thread for filtering records based on some filters is being used.
I am using listview to show data for dataset I get as per above.
While laoding a file everthing seems good but when I search,the problem exists.
while searching when I apply filters for the 1st time, everything is okay. Now again when I apply filters, data of the previous search is show for 1 sec(kind of flicker) and then the new data according to current filter is rendered.
I am using dispatcher for updating UI.
Before I bind any data, I try to clear ListView ItemSource by setting it to null
XAML code i am using is :
{
<ListView Name="listView1"
Grid.Row="0"
ItemsSource= "{Binding ElementName=Window1, Path=Entries}"
AllowDrop="True"
SelectionChanged="listView1_SelectionChanged"
Drop="listView1_Drop">
<ListView.View>
<GridView x:Name="GridView1" >
<GridViewColumn Header="Item" DisplayMemberBinding="{Binding Item}" />
<GridViewColumn Header="TimeStamp" DisplayMemberBinding="{Binding TimeStamp}"/>
<GridViewColumn Header="">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding Image}" Width="16" Height="16" VerticalAlignment="Top" HorizontalAlignment="Left"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Level" DisplayMemberBinding="{Binding Level}"/>
<GridViewColumn Header="Class" DisplayMemberBinding="{Binding Class}"/>
<GridViewColumn Header="Message" DisplayMemberBinding="{Binding Message1}" Width="250" />
</GridView>
</ListView.View>
</ListView>
}
Code Behind is :
private void ClearListView()
{
UpdateList update = new UpdateList(UpdateListData);
List<LogEntry> searcheddata = new List<LogEntry>();
searcheddata = null;
listView1.ItemsSource = null;
listView1.Items.Clear();
listView1.Items.Refresh();
pdDispatcher.BeginInvoke(update, DispatcherPriority.Render, searcheddata);
}
psdispatcher is the dispatcher used to update the UI & searchdata is function to assign datasource to 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).
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]
Hi I have next XAML markup (just a piece of course):
<Window.Resources>
<GridView x:Key="gridview">
<GridViewColumn Width="200" DisplayMemberBinding="{Binding Title}">
<GridViewColumnHeader Content="Titel" Tag="Title" Click="SortClick"/>
</GridViewColumn>
<GridViewColumn Width="200" DisplayMemberBinding="{Binding Artist}">
<GridViewColumnHeader Content="Artiest" Tag="Artist" Click="SortClick"/>
</GridViewColumn>
<GridViewColumn Width="200" DisplayMemberBinding="{Binding Album}">
<GridViewColumnHeader Content="Album" Tag="Album" Click="SortClick"/>
</GridViewColumn>
<GridViewColumn Width="50" DisplayMemberBinding="{Binding Genre}">
<GridViewColumnHeader Content="Genre" Tag="Genre" Click="SortClick"/>
</GridViewColumn>
<GridViewColumn Width="50" DisplayMemberBinding="{Binding Jaar}">
<GridViewColumnHeader Content="Jaar" Tag="Jaar" Click="SortClick"/>
</GridViewColumn>
</GridView>
<my:IdToImageConverter x:Key="idToImageConverter" />
<DataTemplate x:Key="iconTemplate">
...
</DataTemplate>
<my:PlainView x:Key="iconView"
ItemTemplate="{StaticResource iconTemplate}"
ItemWidth="300"/>
</Window.Resources>
<Grid>
<ListView Name="LibView" ItemsSource="{Binding ElementName=win, Path=Biblio}"/>
</Grid>
Biblio is the observable collection containing the elements for LibView
iconView and gridview are the views i use for LibView. This works perfectly.
Now i want to add a view wich groups every element by "Artist", and i want to display this like:
Artist1 - TitleA - AlbumA
- TitleB - AlbumB
- TitleC - AlbumC
Artist2 - TitleD - AlbumD
...
I guess i have to make some kind of template but i don't even know if this is possible using XAML.
It sound like you want a GroupStyle - this might explain:
http://bea.stollnitz.com/blog/?p=17
There are many examples - try a search on wpf groupstyle :)