I'm binding to an ienumerable(items) in my MainViewModel to display data. As i've described here before How to change the content in a datagrid or listview using MVVM, all i want to do is display different tables. My first approach was to use the normal datagrid and set the "AutoGenerateColumns" to true, so that the correct columns are displayed. As it turns out, performance was pretty bad so i switched to ListView and GridView, but since there is no "AutoGenerateColumns" available i need to somehow create and change the columns.
So how would you do it?
Have a look at this question for something that comes close to -- but is not -- auto-generation: WPF GridView with a dynamic definition.
Generally speaking, autogenerating means doing reflection on the ItemsSource, which can get messy since it needs to cover a huge number of possibilities correctly.
Related
I have a ListView with a GridView (multiple columns) layout. Some column have converters which uses the current locale to prettify numbers, turn unix timestamps into datetime strings, or just translate an enum to a localized description of it.
The locale can be changed during runtime, so I need a way to rerun these converters as that happens. Note that the value itself of the binding has not changed, but the output of the converter may be different with a different locale.
What is the best way to do so? I don't want to iterate the whole collection of every affected list and call OnPropertyChanged. Is there a way to either force the ListView to refresh every binding, or just the bindings of some columns?
I found the answer:
ICollectionView view = CollectionViewSource.GetDefaultView(MyListView.ItemsSource);
view.Refresh();
You can force the ListView to update the ItemsSource binding with the following:
lview.GetBindingExpression(ListView.ItemsSourceProperty).UpdateTarget();
Using this same syntax, you could certainly get more specific, and force an update only on the bindings for specific columns, but depending on the number of rows in your ListView, you may not see a performance difference.
I am quite new to WPF and XAML but my background is of ASP.NET and C# so I have a vague idea of how it works.
In .net, I can use the repeater, datalist, gridview, bind to them a DataTable and output from that DataTable. I am now wanting to do the same with WPF.
Basically, I want to display simple records from a database (preferably using a DataTable as I usually work with those). The list might be something like this with two columns
1) Grey TV
2) Red car
3) Blue motorbike
I have looked around but I can't get a definitive answer on what control to use. Some people say ItemsControl and some people say DataGrid. Can anyone help me here?
Thanks in advance.
A DataGrid is used for displaying Table-like data (Per record multiple columns). An ItemsControl is used to display data using your own ItemTemplate, in which you are unlimited in how to represent the items and in what directions or alignments.
Another good useable control for you might be a ListView, which works just as a ListBox except it doesn't have any selection logic. And you can choose between four different ways of displaying your items using the View property (http://msdn.microsoft.com/en-us/library/system.windows.forms.view.aspx).
In your case I would suggest using a ListView.
To bind any items to the control, you have to set the DataContext on the UserControl or the Control itself. And then bind the ItemsSource property to a local List or Collection using the Binding markup extension (http://msdn.microsoft.com/en-us/library/ms750413.aspx). To learn more about data-binding go here:
http://msdn.microsoft.com/en-us/library/ms752347.aspx
I use SQLite3 to read data from a database and then display the results in a listBox, I would to ask if there is possibility to display the results in a listBox as columns in nice show without using DataGrid.
It is better to use ListView instead of listBox.
Also you can still use listbox for multicolumn purpose
Listbox is not designed to work with multiple columns. Any reason why you do not want to use a DataGrid?
In general a grid control is the most appropriate when dealing with items or records having multiple properties or fields to be shown in different columns, some people also use a ListView with the report/details style and several sub items one per each column.
In my experience when the final result is really a table/grid it is easier and simpler to use a grid instead of "forcing" a ListView to behave like a grid. ListView is not designed for that even if it can be used in a similar way.
I'm watching at this page:
http://leeontech.wordpress.com/2010/02/01/summary-row-in-datagrid/
But they're using silverlight. I'm trying to create that user control to use it in a WPF C# application. I mean, not using Silverlight. But I can't find the namespaces: GroupHeader
I'm having a hard time with this. Thanks in advance.
Okay listen, you can totally do this, and in some scenarios I even recommend it.
Using a CollectionViewSource you can easily group your data. In the HeaderTemplate you can even use an Expander (or make your own) and get the animation you might be wanting. Here's a link to a sample of this: http://jerrytech.blogspot.com/2010/06/wpf-data-presentation-step-by-step.html
Using an ItemsControl, you can easily present your groups and details. In the ItemTemplate you can use styles make this look like a grid (if that is really what you want). You can also shift the style based on the type if your collection has more than one type of object in it (eat that datagrid!).
You can wire up your column headers (which will really be custom objects, right?) and handle all the sorting and stuff like that. They will look just right! Not like datagrid WinForm column headers!
Here's what's hard (not impossible, but more coding).
User-resizable columns.
User-rearrangable columns.
New record using bottom, empty row.
Paste from Excel (doesn't work right in datagrid either).
Select Row, highlight Column header.
That's it.
In lots of situations, this is really nice.
For the most part, I cannot stand the datagrid. Too restricting on UX.
I don't think you're not going to be able to get a silverlight control working in WPF.
Adding a footer row to the WPF datagrid is something a lot of people have complained about; it's ridiculous that it wasn't included out of the box.
See this thread from MSDN
Having been through this myself, your best bet will probably be to bite the bullet and use a third party control. It sucks, I know.
I have an ListView set up to sort, and I have that working well.
However, when the ListView is sorted, no indication is provided as to the current sort column and order.
I tried a solution setting the ColumnHeader.ImageKey property whenever sorting, but I want to be able to use images that aren't the size specified by the ListView's SmallImageList.
Is there any way to do this without using OwnerDraw mode?
I don't think you can do it without OwnerDraw. However, OwnerDraw isn't as bad as you might think. There are many examples on CodeProject.com; like this and this