WPF ListBox that can sort by column


I have a XAML file with only a ListBox. I dynamically create the columns and add the rows. with this code:

ListBoxItem l1 = new ListBoxItem();
StackPanel s1 = new StackPanel(); 
s1.Orientation = Orientation.Horizontal;
ContentPresenter ch1 = new ContentPresenter();
ch1.Content = "ICR";
s1.Children.Add(ch1); //just an example I add more than 1 column
li.Content = s1; 

Which works fine, but now I want to create column headers and sort by column. Can I do that with a dynamically with a ListBox or am I going down the wrong path?

2010-09-16 21:42:39 Scores:0

2 answers

Answer 1
J Edward Ellis H
2010-09-16 21:51:24
Answer 2

If you want multiple columns and column headers, don't use a ListBox... use a ListView or a DataGrid instead.

As for sorting, you will probably have to do it manually, because of the way you fill the list. It would be much easier if you were using data binding. About everything in WPF is easier when you start using binding...

Thomas Levesque H
2010-09-16 22:26:41
Share |
View original post at stackoverflow.com

Related topics

Best way to make WPF ListView/GridView sort on column-header clicking?

There are lots of solutions on the internet attempting to fill this seemingly very-basic omission from WPF. I'm really confused as to what would be the "best" way. For example... I want there to be little up/down arrows in the column header to indicate sort direction. There are apparently like ...

WPF Datagrid sort on column with null elements

I have a WPF Datagrid that I am using with a number of columns. One of the columns has some elements that are sometimes null and this causes an exception when I try and sort on this column. The definitions of the columns is something like: <dg:DataGrid.Columns> <dg:DataGridTextColumn Binding="{Binding MyObject.Field1}" Header="Field1" ...

How to add arrow to each WPF listview column to indicate sort direction

I have a gridview ...with multiple columns. If I click on one column, it sorts the gridview based on that column values. I like to add an arrow in that "clicked" column header(when user clicks) to show that "this particular column is clicked and this is the sort direction" How can ...

WPF datagrid : how to sort a column programatically?

How do I sort a column programatically ? I would like to have a function such as myWPFDataGrid.Columns[0].Sort(..) MadSeb ...

How can I sort a bound ListBox with custom sort logic?

I'd like to sort it from the View instead of in the ViewModel. Every example I've seen uses a SortDescription, but it's not customizable. ...

WPF4 Datagrid doesn't sort on column headers

I am trying to implement my first WPF application using an MVVM design pattern. I created an application that is databinding to an ObservableCollection<>. The application renders fine, but I expect the datagrid to re-sort the rows when I click on the column headers. I've researched posts that said: The data source ...

How do I sort a GridView column When a header Is clicked?

I am following instructions from this MSDN article: How to: Sort a GridView Column When a Header Is Clicked But, I'm binding Entity Framework objects to the ListView. I get a runtime NotSupportedException "Specified method is not supported" when the Sort() method fails to execute this line: dataView.SortDescriptions.Clear(); How do I need to ...

How to programaticaly sort an autogenerated column in wpf datagridview..?

I used an List as source. The columns are auto generated. I can sort it in List. But I want to sort programmatically, Lets say 3rd column of data-grid view. How can I do it...? ...

How to sort items in ListView by column bound to an Observable Collection?

How to sort items in ListView by column bound to an Observable Collection ? I've looked all over the place and I can't find anything easy enough or simple on this. ...

How to sort all but first column in WPF DataGrid

I have a WPF DataGrid that in its first column holds a number that is the "ranking" or "position". What I want is that when I click on a column to sort the table based on that column, for the first column to stay as-is. For example: Position Name ...