就不得不嘗試這一個,奇怪,但有趣的問題:-)設法得到它通過使用下面的工作。 對不起,很長的答案,可能的方法來詳細:-)
首先,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