This project has moved and is read-only. For the latest updates, please go here.

How to sort a column by a different property than the displayed property?

Jul 7, 2015 at 11:27 AM
Hi,

I do have an enum reflecting the processing state of an item (such as "Undefined", "UnderConstruction", "Finished" etc.). The item exposes both the enum and a user-friendly text for each value (i.e. "undefined", "under construction", "finished"). The grid displays the user-friendly text, BUT I want to sort by the enum's values - as these reflect the correct processing order.

How can that be done?

Thanks,
Thorsten
Developer
Jul 8, 2015 at 9:10 PM
Hi,

You can still work with the property of the Column. Simply use a CellContentTemplate to display something different for a column. You will still be able to sort by that property.
 <Window.Resources>
      <local:EnumConverter x:Key="EnumConverter" />
      
      <DataTemplate x:Key="EnumCellContentTemplate">
         <TextBlock Text="{Binding ., Converter={StaticResource EnumConverter}}" />
      </DataTemplate>
   </Window.Resources>
    
   <Grid>
      <xcdg:DataGridControl x:Name="_grid">
         <xcdg:DataGridControl.Columns>
            <xcdg:Column FieldName="Name" />
            <xcdg:Column FieldName="MyEnum"
                         CellContentTemplate="{StaticResource EnumCellContentTemplate}" />
         </xcdg:DataGridControl.Columns>
      </xcdg:DataGridControl>
   </Grid>
 public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();

      _grid.ItemsSource = new List<MyData>()
      {
        new MyData() { Name = "First", MyEnum = EnumState.Undefined },
        new MyData() { Name = "Second", MyEnum = EnumState.UnderConstruction },
        new MyData() { Name = "Third", MyEnum = EnumState.Finished },
        new MyData() { Name = "Fourth", MyEnum = EnumState.Undefined },
        new MyData() { Name = "Fifth", MyEnum = EnumState.Undefined },
        new MyData() { Name = "Sixth", MyEnum = EnumState.Finished },
        new MyData() { Name = "Seventh", MyEnum = EnumState.Finished },
        new MyData() { Name = "Eight", MyEnum = EnumState.UnderConstruction },
      };
    }
  }

  public enum EnumState
  {
    Undefined,
    UnderConstruction,
    Finished
  }

  public class MyData
  {
    public string Name
    {
      get;
      set;
    }
    public EnumState MyEnum
    {
      get;
      set;
    }
  }

  public class EnumConverter : IValueConverter
  {
    public object Convert( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
    {
      if( value != null )
      {
        switch( (EnumState)value )
        {
          case EnumState.Undefined:
            return "undefined";
          case EnumState.UnderConstruction:
            return "under construction";
          default:
            return "finished";
        }
      }
      return null;
    }

    public object ConvertBack( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
    {
      throw new NotImplementedException();
    }
  }
Marked as answer by hessetho on 7/12/2015 at 11:32 PM
Jul 13, 2015 at 7:33 AM
Hi,

thanks for this tip! This worked fine for me!

Regards,
Thorsten