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 a form based application?

1 Answers  


What type of elements can be hosted in a ToolBar?

1 Answers  


What are the core wpf assemblies?

1 Answers  


Explain Routed events in WPF?

1 Answers  


What is windows application example?

1 Answers  


What is the entry point of wpf application?

1 Answers  


What are the names of main assemblies used by WPF?

1 Answers  


Can you explain the overall architecture of wpf?

1 Answers  


What are binary resources in wpf?

1 Answers  


In Which NameSpace you will find a ‘Popup’ and ‘Thumb’ control in WPF?

1 Answers  


What are the advantages of wpf over windows forms?

1 Answers  


What is the difference between silverlight and wpf browser application?

1 Answers  


Categories