开发者

List box item Binding from ObservableCollection

开发者 https://www.devze.com 2023-03-21 09:22 出处:网络
I have an ObservableCollection of File as ObservableCollection>FileList> Filelist; The FileList entity class have two properties FileName and FilePath.I want to bind the filenames in开发者_如何学Goto

I have an ObservableCollection of File as

ObservableCollection>FileList> Filelist; The FileList entity class have two properties FileName and FilePath.I want to bind the filenames in开发者_如何学Goto a listbox and While selecting an item(Filename) in the list box need to display the content of the file into a textblock. I am Following MVVM Pattern.

How can i implement this..?

Thanks in Advance..


I have crated a sample with MVVM..

Xaml Code:

<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();

here are your view models

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

精彩评论

暂无评论...
验证码 换一张
取 消