Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
875
Implement SortComparer in xamDataPresenter?
posted

Anyone have an example of how to use a SortComparer for a field?  I have a field that shows Heights, but is being sorted as String.  Therefore, after clicking the Height column header, I get results like (where I really want to sort numerically, not by string).  I cannot just change the underlying value type to integer either, it must remain strings, but I figure i could Parse the values to an int in the SortComparer or something?  How do i interupt the sorting when the user clicks the column header?

9 ft

13 ft

12 ft

11 ft

10 ft

Here is the Xaml:

<Window x:Class="XamDataGridSorting.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:XamDataGridSorting"
        Title="MainWindow" Height="350" Width="525" 
xmlns
:igDP="http://infragistics.com/DataPresenter">     <Grid>         <igDP:XamDataGrid DataSource="{Binding}" Name="xamDataGrid1"
VerticalAlignment
="Top" >                      </igDP:XamDataGrid>     </Grid> </Window>

Here is the code:

namespace XamDataGridSorting
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            ObservableCollection<Tree> data = 
new ObservableCollection<Tree>();             data.Add(new Tree() { Name = "Apple", Height = "9 ft" });             data.Add(new Tree() { Name = "Pear", Height = "10 ft" });             data.Add(new Tree() { Name = "Cherry", Height = "11 ft" });             data.Add(new Tree() { Name = "Palm", Height = "12 ft" });             data.Add(new Tree() { Name = "Oak", Height = "13 ft" });             this.DataContext = data;         }     }     public class Tree     {         public string Height { getset; }         public string Name { getset; }     } }
XamDataGridSorting.zip