2010-10-19 91 views
3

我有如下定義WPF:綁定到一個List類

public class File 
{ 
public string FileName {set;get;} 
public List<Property> PropertyList; 
} 

這裏一個File類裏面是房產類的樣子:

public class Property 
{ 
public string PropertyName { set; get;}; 
public string PropertyValue { set; get;}; 
... 
} 

我需要綁定一個List<File>到DataGrid,顯示FileName。另外,我想爲PropertyList, 中的每個Property創建一個列,並將字符串值PropertyName作爲列標題,並將PropertyValue的字符串值作爲列的值。

這是可能的WPF?

感謝,

+1

希望你真正的類名沒有文件和屬性... – 2010-10-19 19:37:37

回答

1

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

首先,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。

alt text

+0

這是真棒,謝謝。 – sean717 2010-10-19 22:17:25