2014-11-03 3 views
0

Я пытаюсь связать DataGrid с List, поэтому он автоматически обновляется, когда список делает.WPF DataGrid привязывается к свойствам класса внутри списка

В моих xaml.cs:

private DataStorage dataStorage = new DataStorage(); 

XAML:

<DataGrid Name="DropFilesDataGrid" AutoGenerateColumns="False" ItemsSource="{Binding}"> 
    <DataGrid.Columns> 
     <DataGridTextColumn x:Name="colFileName" Binding="{Binding fileName}" Header="File Name" /> 
     <DataGridTextColumn x:Name="colFileType" Binding="{Binding fileType}" Header="File Type" /> 
     <DataGridTextColumn x:Name="colFileLocation" Binding="{Binding fileLocation}" Header="File Location" /> 
    </DataGrid.Columns> 
</DataGrid> 

DataStorage В классе:

public List<File> listOfFiles = new List<File>(); 

и класса File:

private string fileName { get; set; } 
private long fileSize { get; set; } 
private string fileImage { get; set; } 
private string fileLocation { get; set; } 
private string fileType { get; set; } 

добавить новые файлы в список с помощью:

foreach (string fileDropped in files) 
{ 
    File file = new File(fileDropped); 
    dataStorage.AddFileToList(file); 
} 

Список наполняется этими файловыми объектами и свойства не нуль.

Моя проблема заключается в получении DataGridView, чтобы обновить его?

Моя ошибка:

System.Windows.Data Error: 40 : BindingExpression path error: 'fileName' property not found on 'object' ''File' (HashCode=1820782)'. BindingExpression:Path=fileName; DataItem='File' (HashCode=1820782); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') 
System.Windows.Data Error: 40 : BindingExpression path error: 'fileType' property not found on 'object' ''File' (HashCode=1820782)'. BindingExpression:Path=fileType; DataItem='File' (HashCode=1820782); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') 
System.Windows.Data Error: 40 : BindingExpression path error: 'fileLocation' property not found on 'object' ''File' (HashCode=1820782)'. BindingExpression:Path=fileLocation; DataItem='File' (HashCode=1820782); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') 

ответ

1

Использование ObservableCollection<File> вместо List<File> А также использовать public член класса File.

public ObservableCollection<File> listOfFiles = new ObservableCollection<File>(); 

И

public string fileName { get; set; } 
    public long fileSize { get; set; } 
    public string fileImage { get; set; } 
    public string fileLocation { get; set; } 
    public string fileType { get; set; } 
+0

Вы изменили член класса 'File' в качестве публики? –