How to change column's header caption on Infragistics XamDataGrid

Noich picture Noich · Nov 19, 2012 · Viewed 10.1k times · Source

It seems a bit trivial, but I have a XamDataGrid in my WPF app that I'd like to set its columns' caption myself and can't.

The binding is to BindingList<DictionaryEntry> (not the .Net one, but still has only Key and Value properties.

The columns are named Key and Value (and updating if I change the properties' names in the DictionaryEntry class), while I'd like to set them in the XAML: (The FieldsLayout section)

<igDP:XamDataGrid Grid.Row="1"
                  Grid.Column="0"
                  Name="ResultStructure"
                  DataSource="{Binding Path=VariablesDictionary, Mode=TwoWay, ValidatesOnExceptions=True}"
                  Theme="Aero"
                  GroupByAreaLocation="None"
                  IsEnabled="{Binding CanEditStructure}"
                  CellUpdating="ResultStructure_OnCellUpdating"
                  CellUpdated="ResultStructure_OnCellUpdated">
  <igDP:XamDataGrid.FieldLayoutSettings>
    <igDP:FieldLayoutSettings AllowClipboardOperations="All"
                              AllowFieldMoving="WithinLogicalRow"
                              AllowAddNew="True"
                              AddNewRecordLocation="OnBottom"
                              AllowDelete="True" />
  </igDP:XamDataGrid.FieldLayoutSettings>
  <igDP:XamDataGrid.FieldLayouts>
    <igDP:FieldLayout>
      <igDP:FieldLayout.Fields>
        <igDP:Field Name="Column" Tag="Column" Width="Auto" />
        <igDP:Field Tag="Variable" Name="Variable" Width="Auto" />
      </igDP:FieldLayout.Fields>
    </igDP:FieldLayout>
  </igDP:XamDataGrid.FieldLayouts>
</igDP:XamDataGrid>

Thanks.

Answer

punker76 picture punker76 · Nov 19, 2012

there is a Label property for this issue

<igDP:Field Name="propertyName" Label="Your custom column name" />

EDIT 2

here is a complete example that works for me (note: you need AutoGenerateFields="False") now with a viewmodel.

<Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:igDP="http://infragistics.com/DataPresenter"
        Title="Window1"
        Height="400"
        Width="600">
  <Grid>
    <igDP:XamDataGrid DataSource="{Binding SampleDataList}"
                      Theme="Aero"
                      GroupByAreaLocation="None">
      <igDP:XamDataGrid.FieldLayoutSettings>
        <igDP:FieldLayoutSettings AllowClipboardOperations="All"
                                  AllowFieldMoving="WithinLogicalRow"
                                  AllowAddNew="True"
                                  AddNewRecordLocation="OnBottom"
                                  AutoGenerateFields="False"
                                  AllowDelete="True" />
      </igDP:XamDataGrid.FieldLayoutSettings>
      <igDP:XamDataGrid.FieldLayouts>
        <igDP:FieldLayout>
          <igDP:FieldLayout.Fields>
            <igDP:Field Name="Sample1"
                        Label="Custom Label Spalte 1"
                        Width="Auto" />
            <igDP:Field Name="Sample2"
                        Label="Custom Label Spalte 2"
                        Width="Auto" />
          </igDP:FieldLayout.Fields>
        </igDP:FieldLayout>
      </igDP:XamDataGrid.FieldLayouts>
    </igDP:XamDataGrid>
  </Grid>
</Window>

code behind and the viewmodel using a BindingList

public partial class Window1 : Window
{
  public Window1() {
    this.DataContext = new SampleDataViewModel();
    this.InitializeComponent();
  }
}

public class SampleDataViewModel : DependencyObject
{
  public class SampleData : INotifyPropertyChanged
  {
    private string sample1;
    private string sample2;

    public string Sample1 {
      get { return this.sample1; }
      set {
        if (Equals(value, this.sample1)) {
          return;
        }
        this.sample1 = value;
        this.OnPropertyChanged("Sample1");
      }
    }

    public string Sample2 {
      get { return this.sample2; }
      set {
        if (Equals(value, this.sample2)) {
          return;
        }
        this.sample2 = value;
        this.OnPropertyChanged("Sample2");
      }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName) {
      var handler = this.PropertyChanged;
      if (handler != null) {
        handler(this, new PropertyChangedEventArgs(propertyName));
      }
    }
  }

  public static readonly DependencyProperty SampleDataListProperty =
    DependencyProperty.Register("SampleDataList", typeof(BindingList<SampleData>), typeof(Window1), new PropertyMetadata(default(BindingList<SampleData>)));

  public BindingList<SampleData> SampleDataList {
    get { return (BindingList<SampleData>)this.GetValue(SampleDataListProperty); }
    set { this.SetValue(SampleDataListProperty, value); }
  }

  public SampleDataViewModel() {
    this.SampleDataList = new BindingList<SampleData>();
    for (int i = 0; i < 10; i++) {
      var data = new SampleData();
      data.Sample1 = "Test sample Text " + i % 100;
      data.Sample2 = "Another Test " + i % 200;
      this.SampleDataList.Add(data);
    }
  }
}

hope that helps