wpf 如何在 Infragistics XamDataGrid 上更改列的标题标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13454331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change column's header caption on Infragistics XamDataGrid
提问by Noich
It seems a bit trivial, but I have a XamDataGridin my WPF app that I'd like to set its columns' caption myself and can't.
这似乎有点微不足道,但XamDataGrid我的 WPF 应用程序中有一个我想自己设置其列的标题,但不能。
The binding is to BindingList<DictionaryEntry>(not the .Netone, but still has only Keyand Valueproperties.
绑定是到BindingList<DictionaryEntry>(不是.Net一个,但仍然只有Key和Value属性。
The columns are named Keyand Value(and updating if I change the properties' names in the DictionaryEntryclass), while I'd like to set them in the XAML: (The FieldsLayoutsection)
列被命名Key和Value(如果我更改类中的属性名称,则会更新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>
Thanks.
谢谢。
回答by punker76
there is a Label property for this issue
此问题有一个 Label 属性
<igDP:Field Name="propertyName" Label="Your custom column name" />
EDIT 2
编辑 2
here is a complete example that works for me (note: you need AutoGenerateFields="False") now with a viewmodel.
这是一个适用于我的完整示例(注意:您需要 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>
code behind and the viewmodel using a BindingList
使用 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
希望有帮助

