2011-07-20 63 views
1

我有一個ObservableCollection的文件爲列表框項綁定來自ObservableCollection

ObservableCollection> FileList> Filelist; FileList實體類有兩個屬性FileName和FilePath.I想要將文件名綁定到列表框中,並且在列表框中選擇一個項目(文件名)時需要將文件的內容顯示到文本塊中。 我正在關注MVVM模式。

我該如何實現這個..?

在此先感謝..

+0

我們可以假設像..所有文件都是txt文件..? – Bathineni

+0

所有文件都是txtFiles ... – S007

回答

2

我已經裝箱與MVVM樣本..

XAML代碼:

<Grid Name="layoutRoot"> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition></ColumnDefinition> 
     <ColumnDefinition></ColumnDefinition> 
    </Grid.ColumnDefinitions> 
    <ListBox ItemsSource="{Binding FilesCollection}" SelectedItem="{Binding SelectedFile}"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <TextBlock Text="{Binding FileName}"></TextBlock> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
    <TextBlock Text="{Binding FileContent}" Grid.Column="1"></TextBlock> 
</Grid> 

XAML代碼背後:

InitializeComponent(); 
      this.DataContext = new ViewModel(); 

這裏有你查看模型

public class ViewModel : ViewModelBase 
{ 
    public ObservableCollection<Fileinfo> FilesCollection { get; set; } 
    private string _fileContent; 

    public string FileContent 
    { 
     get { return _fileContent; } 
     set 
     { 
      _fileContent = value; 
      OnPropertyChanged("FileContent"); 
     } 
    } 
    private Fileinfo _selectedFile; 

    public Fileinfo SelectedFile 
    { 
     get { return _selectedFile; } 
     set 
     { 
      _selectedFile = value; 
      OnPropertyChanged("SelectedFile"); 
      GetFileCotnent(); 
     } 
    } 

    private void GetFileCotnent() 
    { 
     using (StreamReader sr = new StreamReader(SelectedFile.FilePath)) 
     { 
      FileContent = sr.ReadToEnd(); 
     } 
    } 

    public ViewModel() 
    { 
     FilesCollection = new ObservableCollection<Fileinfo>(); 
     string[] files = Directory.GetFiles(@"C:\files"); 
     foreach (var item in files) 
     { 
      FilesCollection.Add(new Fileinfo(item.Substring(item.LastIndexOf('\\')), item)); 
     } 
    } 

} 

public class Fileinfo : ViewModelBase 
{ 
    public Fileinfo(string filename, string filepath) 
    { 
     this.FileName = filename; 
     this.FilePath = filepath; 
    } 
    private string _fileName; 

    public string FileName 
    { 
     get 
     { 
      return _fileName; 
     } 
     set 
     { 
      _fileName = value; OnPropertyChanged("FileName"); 
     } 
    } 

    private string _filePath; 

    public string FilePath 
    { 
     get { return _filePath; } 
     set 
     { 
      _filePath = value; 
      OnPropertyChanged("FilePath"); 
     } 
    } 

} 

public class ViewModelBase : INotifyPropertyChanged 
{ 
    protected virtual void OnPropertyChanged(string propertyName) 
    { 
     if (this.PropertyChanged != null) 
     { 
      this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
    public event PropertyChangedEventHandler PropertyChanged; 
} 
+0

感謝它的工作.. – S007

+0

現在我想擴展它以使用菜單顯示來自不同文件夾的文件..使用相同的代碼顯示錯誤,當我點擊第二個菜單項時,選定的項目是空的..可以üPLZ看看這個.. – S007

+0

添加一個空檢查GetFileCotnent或在調用GetFileCotnent之前... – Bathineni

相關問題