Quantcast
Channel: wpftoolkit Discussions Rss Feed
Viewing all articles
Browse latest Browse all 2157

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

$
0
0
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();
    }
  }

Viewing all articles
Browse latest Browse all 2157

Trending Articles