mvvmを使用して動的にデータグリッドを生成したいとします。データグリッドの各セルでは、私はオブジェクトを表示する必要があります。列名は、オブジェクトのプロパティの1つです。データグリッドのItemsourceはオブジェクトのリストになります。 mvvmを使って動的にDataGridを生成するには?mvvmを使用してwpfデータグリッドでカラムを動的に生成する方法
更新
私がICustomTypeDescriptorをusiing拡張されているカスタムクラスを作成しました。
public class MyCustomType : ICustomTypeDescriptor
{
// This is instance data.
private readonly BindingList<PropertyDescriptor> _propertyDescriptors = new BindingList<PropertyDescriptor>();
// The data is stored on the type instance.
private readonly IDictionary<string, object> _propertyValues = new Dictionary<string, object>();
// The property descriptor now takes an extra argument.
public void AddProperty(string name, Type type)
{
_propertyDescriptors.Add(new MyPropertyDescriptor(name, type));
}
public PropertyDescriptorCollection GetProperties()
{
return new PropertyDescriptorCollection(_propertyDescriptors.ToArray());
}
public PropertyDescriptorCollection GetProperties(Type type)
{
return new PropertyDescriptorCollection(_propertyDescriptors.ToArray());
}
public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
return GetProperties();
}
public object GetPropertyOwner(PropertyDescriptor pd)
{
throw new NotImplementedException();
}
public AttributeCollection GetAttributes()
{
throw new NotImplementedException();
}
public string GetClassName()
{
throw new NotImplementedException();
}
public string GetComponentName()
{
throw new NotImplementedException();
}
public TypeConverter GetConverter()
{
throw new NotImplementedException();
}
public EventDescriptor GetDefaultEvent()
{
throw new NotImplementedException();
}
public PropertyDescriptor GetDefaultProperty()
{
throw new NotImplementedException();
}
public object GetEditor(Type editorBaseType)
{
throw new NotImplementedException();
}
public EventDescriptorCollection GetEvents()
{
return null;
}
public EventDescriptorCollection GetEvents(Attribute[] attributes)
{
return null;
}
private class MyPropertyDescriptor : PropertyDescriptor
{
// This data is here to indicate that different instances of the type
// object may have properties of the same name, but with different
// characteristics.
private readonly Type _type;
public MyPropertyDescriptor(string name, Type type)
: base(name, null)
{
_type = type;
}
public override bool CanResetValue(object component)
{
throw new NotImplementedException();
}
public override Type ComponentType
{
get { throw new NotImplementedException(); }
}
public override object GetValue(object component)
{
MyCustomType obj = (MyCustomType) component;
object value = null;
obj._propertyValues.TryGetValue(Name, out value);
return value;
}
public override bool IsReadOnly
{
get { return false; }
}
public override Type PropertyType
{
get { return _type; }
}
public override void ResetValue(object component)
{
throw new NotImplementedException();
}
public override void SetValue(object component, object value)
{
var oldValue = GetValue(component);
if (oldValue != value)
{
MyCustomType obj = (MyCustomType) component;
obj._propertyValues[Name] = value;
OnValueChanged(component, new PropertyChangedEventArgs(Name));
}
}
public override bool ShouldSerializeValue(object component)
{
throw new NotImplementedException();
}
public override void AddValueChanged(object component, EventHandler handler)
{
// set a breakpoint here to see WPF attaching a value changed handler
base.AddValueChanged(component, handler);
}
}
}
このカスタムタイプオブジェクトのリストをdatagrid itemsourceにバインドします。しかし、DataGridはコンテンツを表示していませんか?
ありがとうございましたDaniel、実際には、各行オブジェクトには、セルのオブジェクトのリストがあります。各セルオブジェクトには複数のプロパティがあります。私は列名としてプロパティ値の1を表示したいと思い、各セルオブジェクトは、オブジェクトのタイプに基づいて異なるセルスタイルを持っています。どのようにこれらの多くの条件でデータグリッドを動的に作成するのですか? – Rony