就不得不尝试这一个,奇怪,但有趣的问题:-)设法得到它通过使用下面的工作。 对不起,很长的答案,可能的方法来详细:-)
首先,DataGrid。平原和简单
<DataGrid Name="c_dataGrid" AutoGenerateColumns="False"
CanUserAddRows="False" CanUserDeleteRows="False"/>
然后在文件类,叫MyFile
public class MyFile
{
public MyFile() : this(string.Empty) {}
public MyFile(string fileName)
{
FileName = fileName;
MyPropertyList = new ObservableCollection<MyProperty>();
}
public string FileName
{
set;
get;
}
public ObservableCollection<MyProperty> MyPropertyList
{
get;
set;
}
}
属性类,名为myProperty的
public class MyProperty
{
public MyProperty() : this(string.Empty, string.Empty) {}
public MyProperty(string propertyName, string propertyValue)
{
MyPropertyName = propertyName;
MyPropertyValue = propertyValue;
}
public string MyPropertyName
{
set;
get;
}
public string MyPropertyValue
{
set;
get;
}
}
一个包含列表MYFILES
public ObservableCollection<MyFile> MyFileList{ get; set; }
创造了一些假的数据到popula TE列表和设置的ItemsSource DataGrid上
MyFile myFile1 = new MyFile("MyFile1");
myFile1.MyPropertyList.Add(new MyProperty("Name1", "Value1"));
myFile1.MyPropertyList.Add(new MyProperty("Name2", "Value2"));
myFile1.MyPropertyList.Add(new MyProperty("Name3", "Value3"));
MyFile myFile2 = new MyFile("MyFile2");
myFile2.MyPropertyList.Add(new MyProperty("Name1", "Value1"));
myFile2.MyPropertyList.Add(new MyProperty("Name4", "Value4"));
myFile2.MyPropertyList.Add(new MyProperty("Name5", "Value5"));
MyFileList = new ObservableCollection<MyFile>();
MyFileList.Add(myFile1);
MyFileList.Add(myFile2);
c_dataGrid.ItemsSource = MyFileList;
增加了DataGridTextColumn的文件名属性
c_dataGrid.Columns.Add(GetNewMyFileNameColumn());
private DataGridColumn GetNewMyFileNameColumn()
{
DataGridTextColumn myFileNameColumn = new DataGridTextColumn();
myFileNameColumn.Header = "FileName";
myFileNameColumn.Width = new DataGridLength(1.0, DataGridLengthUnitType.Auto);
Binding valueBinding = new Binding();
valueBinding.Path = new PropertyPath("FileName");
valueBinding.Mode = BindingMode.TwoWay;
valueBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
valueBinding.NotifyOnSourceUpdated = true;
valueBinding.NotifyOnTargetUpdated = true;
myFileNameColumn.Binding = valueBinding;
return myFileNameColumn;
}
再来说MyPropertyList。我只是想一次添加的每个MyPropertyName所以如果我有以下
MyFile1
-Name1
-Name2
-Name3
MyFile2
-Name1
-Name4
-Name5
的生成列应是Name1,Name2,Name3,Name4和Name5。我不得不将MyPropertyName提供给Converter的构造函数,以便知道要查找哪个属性。
最后的转换器
public class MyPropertyConverter : IValueConverter
{
private string m_propertyName = string.Empty;
ObservableCollection<MyProperty> m_myPropertyList = null;
public MyPropertyConverter(string propertyName)
{
m_propertyName = propertyName;
}
object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
m_myPropertyList = value as ObservableCollection<MyProperty>;
if (m_myPropertyList == null)
{
return null;
}
foreach (MyProperty myProperty in m_myPropertyList)
{
if (myProperty.MyPropertyName == m_propertyName)
{
return myProperty.MyPropertyValue;
}
}
return null;
}
object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (m_myPropertyList != null)
{
foreach (MyProperty myProperty in m_myPropertyList)
{
if (myProperty.MyPropertyName == m_propertyName)
{
myProperty.MyPropertyValue = value.ToString();
break;
}
}
}
return m_myPropertyList;
}
}
在转换它将检查指定的MyPropertyName,如果它发现它,返回MyPropertyValue,否则返回null。对于ConvertBack方法也是如此,但它会使用给定的MyPropertyName将MyPropertyValue设置为MyProperty的新值,然后返回列表或null。
不在MyFile列表中的属性将不可编辑,它们将在离开单元格(这可能是点)时变回为空。
这将导致一个看起来像这样的DataGrid。
希望你真正的类名没有文件和属性... – 2010-10-19 19:37:37