Voraussetzungen gezeigt: .Net Framework 4.5.1Anzeigenamen der dynamischen Datagrid-Spalten werden nicht
Ich ITypedList
und ICustomTypeDescriptor
mit Spalten dynamisch machen DataGrid
zu erzeugen. Um benutzerfreundliche Spaltennamen bereitzustellen, erstelle ich Eigenschaftsdeskriptoren, die den Konstruktor DisplayNameAttribute
bereitstellen, wie im folgenden Beispiel gezeigt. Obwohl Debugger zeigt, dass PropertyDescriptor.DisplayName
Eigenschaft erhält, was ich in dem Attribut DataGrid
zur Verfügung gestellt wird, berücksichtigt diesen Wert nicht und zeigt weiterhin den Eigenschaftsnamen anstelle der Eigenschaft Anzeige Name. Irgendwelche Ideen, was ich falsch mache?
Beispiel WDataGridTest.xaml
<Window x:Class="Local.WGridViewTest"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:l="clr-namespace:Local"
Title="WGridViewTest" Height="300" Width="300">
<Window.Resources>
<l:DataTable x:Key="DataTable"/>
</Window.Resources>
<Grid>
<DataGrid ItemsSource="{StaticResource ResourceKey=DataTable}"/>
</Grid>
</Window>
-Code hinter WDataGridTest.xaml.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.ComponentModel;
namespace Local {
/// <summary>Interaction logic for WGridViewTest.xaml</summary>
public partial class WGridViewTest : Window {
public WGridViewTest() {
InitializeComponent();
}
}
public class DataTable : BindingList<DataRow>, ITypedList {
private PropertyDescriptorCollection _PropertyDescriptors;
public DataTable() :
base() {
AllowNew = false;
AllowRemove = false;
AllowEdit = true;
_PropertyDescriptors = new PropertyDescriptorCollection(new PropertyDescriptor[0], false);
_PropertyDescriptors.Add(new DataValuePropertyDescriptor("Column1"));
_PropertyDescriptors.Add(new DataValuePropertyDescriptor("Column2"));
_PropertyDescriptors.Add(new DataValuePropertyDescriptor("Column3"));
Items.Add(new DataRow(this));
Items.Add(new DataRow(this));
Items.Add(new DataRow(this));
Items.Add(new DataRow(this));
Items.Add(new DataRow(this));
}
#region ITypedList implementation
public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] ListAccessors) {
return _PropertyDescriptors;
}
public string GetListName(PropertyDescriptor[] ListAccessors) {
return "Data Table";
}
#endregion ITypedList implementation
}
public class DataRow : ICustomTypeDescriptor {
public DataRow(DataTable DataTable) {
this.DataTable = DataTable;
}
public DataTable DataTable {
get;
private set;
}
public object GetValue(string ColumnName) {
return String.Concat(ColumnName, "@", GetHashCode());
}
public void SetValue(string ColumnName, object Value) {
}
#region ICustomTypeDescriptor implementation
public AttributeCollection GetAttributes() { return AttributeCollection.Empty; }
public string GetClassName() { return GetType().FullName; }
public string GetComponentName() { return GetType().Name; }
public TypeConverter GetConverter() { return null; }
public EventDescriptor GetDefaultEvent() { return null; }
public PropertyDescriptor GetDefaultProperty() { return null; }
public object GetEditor(Type EditorBaseType) { return null; }
public EventDescriptorCollection GetEvents(Attribute[] Attributes) { return EventDescriptorCollection.Empty; }
public EventDescriptorCollection GetEvents() { return EventDescriptorCollection.Empty; }
public PropertyDescriptorCollection GetProperties(Attribute[] Attributes) { return DataTable.GetItemProperties(null); }
public PropertyDescriptorCollection GetProperties() { return DataTable.GetItemProperties(null); }
public object GetPropertyOwner(PropertyDescriptor PropertyDescriptor) { return this; }
#endregion Property value tracking
}
public class DataValuePropertyDescriptor : PropertyDescriptor {
public DataValuePropertyDescriptor(string Name) :
base(Name, new Attribute[] { new DisplayNameAttribute(String.Concat("Display: ", Name)) }) {
}
#region PropertyDescriptor implementation
public override Type ComponentType { get { return typeof(DataRow); } }
public override Type PropertyType { get { return typeof(string); } }
public override bool IsReadOnly { get { return false; } }
public override bool CanResetValue(object DataRow) { return true; }
public override object GetValue(object DataRow) { return ((DataRow)DataRow).GetValue(Name); }
public override void ResetValue(object DataRow) { ((DataRow)DataRow).SetValue(Name, null); }
public override void SetValue(object DataRow, object Value) { ((DataRow)DataRow).SetValue(Name, Value); }
public override bool ShouldSerializeValue(object DataRow) { return false; }
#endregion PropertyDescriptor implementation
}
}
Und das Ergebnis sieht wie folgt aus.
funktioniert für mich mit 4.0 – jHilscher
@jHilscher fein, versuchte 4.0, gleiche Ergebnisse sehen, keine Anzeigenamen. Hast du mein Beispiel getestet oder funktioniert dein Code? –
Ich habe nur Ihre Probe kopiert/eingefügt. – jHilscher