Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6174849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:49:16+00:00 2026-05-23T23:49:16+00:00

I have an ObservableCollection of File as ObservableCollection>FileList> Filelist; The FileList entity class have

  • 0

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 into 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..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:49:17+00:00Added an answer on May 23, 2026 at 11:49 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an instance of ObservableCollection bound to a WPF listbox with two separate
I have a ObservableCollection<Object> that holds two different types of objects: Directory and File
I have an ObservableCollection of addresses that I am binding to a ListBox. Then
I have an ObservableCollection of Task objects. Each Task has the following properties: AssignedTo
I have a collection private ObservableCollection<Contact> _contacts; In the constructor of my class I
I have ItemsControl which I bind to ObservableCollection On my view model I just
I have read that SL4 introduces the ability to data bind properties on objects
Okay, I have an ItemsControl in a XAML file that binds to an ObservableCollection.
In my XAML file, I have a ListBox declared like this : <ListBox x:Name=lstDeck
I have an ObservableCollection<T> . I've bound it to a ListBox control and I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.