【问题标题】:How to change column's header caption on Infragistics XamDataGrid如何更改 Infragistics XamDataGrid 上的列标题标题
【发布时间】:2012-11-19 12:57:04
【问题描述】:

这似乎有点微不足道,但我的 WPF 应用程序中有一个 XamDataGrid,我想自己设置它的列标题,但不能。

绑定到BindingList<DictionaryEntry>(不是.Net之一,但仍然只有KeyValue属性。

这些列被命名为 KeyValue(如果我在 DictionaryEntry 类中更改属性名称,则会更新),而我想在 XAML 中设置它们:(FieldsLayout 部分)

<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>

谢谢。

【问题讨论】:

  • Field 上没有 Label 属性可以为列标题设置自定义文本吗?
  • 有,但不会覆盖绑定类的属性名称。

标签: wpf infragistics xamdatagrid


【解决方案1】:

这个问题有一个标签属性

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

编辑 2

这是一个适合我的完整示例(注意:您需要 AutoGenerateFields="False") 现在有了视图模型。

<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>

后面的代码和使用 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);
    }
  }
}

希望对你有帮助

【讨论】:

  • 不。它似乎无法覆盖属性的名称。我不知道这是否与我的特定绑定有关,Infragistics 的功能浏览器不是很有帮助。
  • @Noich 我想你忘了这个:
  • 我正在使用BindingListObservableCollection 不会让我添加行)并使用您的 XAML 我在应该是 XamDataGrid 的地方得到一个空白。
  • 另外,我想把数据放在视图模型中而不是在后面的代码中,你的建议可以吗?
  • @Noich 我更改了示例,现在使用 viemodel 和绑定列表,一切正常
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-09
  • 2021-04-25
  • 2017-05-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多