列表框项目来自ObservableCollection的绑定

时间:2011-07-20 09:54:06

标签: wpf binding

我有一个ObservableCollection文件为

的ObservableCollection>文件列表>文件清单; FileList实体类有两个属性FileName和FilePath.I想要将文件名绑定到列表框中,而在列表框中选择一个项目(Filename)需要将文件内容显示为文本块。 我正在关注MVVM模式。

我该如何实现这个......?

先谢谢..

1 个答案:

答案 0 :(得分: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 Code Behind:

 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;
}