2010-10-19 90 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