Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


How can I sort items in a ListBox in WPF?



How can I sort items in a ListBox in WPF?..

Answer / Pawan Kumar Gupta

To sort items in a ListBox in WPF, you need to implement the IComparer interface and set it as the SortDescriptions collection's Comparer property on your ItemsControl. Here is an example of how to do that:

```csharp
public class MyClass : IComparer<MyItem>
{
public int Compare(MyItem x, MyItem y)
{
// Compare based on a property of the item
return x.PropertyName.CompareTo(y.PropertyName);
}
}

// Later in your XAML:
<ListBox ItemsSource="{Binding MyItems}">
<ListBox.Resources>
<local:MyClass x:Key="comparer" />
</ListBox.Resources>
<ListBox.SortDescriptions>
<SortDescription SortMemberPath="PropertyName" Comparer="{StaticResource comparer}" />
</ListBox.SortDescriptions>
</ListBox>

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More WPF Interview Questions

what is the use of property element syntax?

1 Answers  


What is the difference between xmlns and xmlns:x in wpf?

1 Answers  


What is the use of system.windows.navigation namespace in wpf?

1 Answers  


What is wpf mvvm?

1 Answers  


What is windows application example?

1 Answers  


Is it right to say that WPF has replaced DirectX?

1 Answers  


What is the use of "system.windows.markup" namespace in wpf?

1 Answers  


What is application object and its reponsiblity?

1 Answers  


How to create class in wpf application?

1 Answers  


How we can enter multiple lines of text in WPF Textbox?

1 Answers   Tech Mahindra,


Name the Layout Panels of WPF?

1 Answers  


When did mvvm introduced?

1 Answers  


Categories