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?

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

No New Questions to Answer in this Category !!    You can

Post New Questions

Answer Questions in Different Category