Wpf 从ObservableCollection绑定列表框项

Wpf 从ObservableCollection绑定列表框项,wpf,binding,Wpf,Binding,我有一个可观察到的文件收集 ObservableCollection>FileList>FileList; FileList实体类有两个属性FileName和FilePath。我想将文件名绑定到列表框中,在列表框中选择项目(文件名)时,需要将文件内容显示到文本块中。 我遵循MVVM模式 我如何实现这一点 提前感谢。我已经用MVVM包装了一个样品 Xaml代码: <Grid Name="layoutRoot"> <Grid.ColumnDefinitions>

我有一个可观察到的文件收集

ObservableCollection>FileList>FileList; FileList实体类有两个属性FileName和FilePath。我想将文件名绑定到列表框中,在列表框中选择项目(文件名)时,需要将文件内容显示到文本块中。 我遵循MVVM模式

我如何实现这一点


提前感谢。

我已经用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>
这是您的视图模型

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;
}
public类ViewModel:ViewModelBase
{
公共ObservableCollection文件集合{get;set;}
私有字符串_fileContent;
公共字符串文件内容
{
获取{return\u fileContent;}
设置
{
_fileContent=值;
OnPropertyChanged(“文件内容”);
}
}
私有文件信息\u选择的文件;
公共文件信息已选择文件
{
获取{return\u selectedFile;}
设置
{
_selectedFile=value;
OnPropertyChanged(“SelectedFile”);
GetFileCotnent();
}
}
私有void getFileContent()
{
使用(StreamReader sr=new StreamReader(SelectedFile.FilePath))
{
FileContent=sr.ReadToEnd();
}
}
公共视图模型()
{
filescolection=新的ObservableCollection();
string[]files=Directory.GetFiles(@“C:\files”);
foreach(文件中的var项)
{
添加(新文件信息(item.Substring(item.LastIndexOf('\\')),item));
}
}
}
公共类文件信息:ViewModelBase
{
公共文件信息(字符串文件名、字符串文件路径)
{
this.FileName=文件名;
this.FilePath=FilePath;
}
私有字符串\u文件名;
公共字符串文件名
{
得到
{
返回_文件名;
}
设置
{
_文件名=值;OnPropertyChanged(“文件名”);
}
}
私有字符串\u文件路径;
公共字符串文件路径
{
获取{return\u filePath;}
设置
{
_filePath=value;
OnPropertyChanged(“文件路径”);
}
}
}
公共类ViewModelBase:INotifyPropertyChanged
{
受保护的虚拟void OnPropertyChanged(字符串propertyName)
{
if(this.PropertyChanged!=null)
{
this.PropertyChanged(this,newpropertychangedventargs(propertyName));
}
}
公共事件属性更改事件处理程序属性更改;
}

我们可以假设像。。所有文件都是txt文件..?确切地说,所有文件都是txt文件..现在我想扩展它,使用一个菜单显示来自不同文件夹的文件..使用相同的代码,当我单击第二个菜单项时显示错误,因为所选项为空..请查看此。。
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;
}