Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Get File Path from ListView

Get File Path from ListView

Scheduled Pinned Locked Moved WPF
wpfcsharpwinformshelp
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MumbleB
    wrote on last edited by
    #1

    Hi Guys, I have a nice working app in Windows Forms. I am attempting to port it to WPF and I am stuck with selecting a row and getting a value from one of the columns. Apologies for the long code: In Windows Forms App I do the below:

        private void lstFiles\_DoubleClick(object sender, EventArgs e)
        {
            if (lstFiles.SelectedItems.Count != 0)
            {
                string file = lstFiles.SelectedItems\[0\].Text;
                openFormGeneral(file);
            }
        }
    

    In WPF .xaml:

    I Populate the ListView as follows:

        public class songDetails
        {
        public string Title { get; set; }
        public string Artist { get; set; }
        public string Track { get; set; }
        public string Set { get; set; }
        public string Album { get; set; }
        public string Genre { get; set; }
        public string Language { get; set; }
        public string FileName { get; set; }
        public string FilePath { get; set; }
        }
    
        public IList songDetail { get; set; }
    
    
        public void AddFile(ID3Info File)
    
    M 1 Reply Last reply
    0
    • M MumbleB

      Hi Guys, I have a nice working app in Windows Forms. I am attempting to port it to WPF and I am stuck with selecting a row and getting a value from one of the columns. Apologies for the long code: In Windows Forms App I do the below:

          private void lstFiles\_DoubleClick(object sender, EventArgs e)
          {
              if (lstFiles.SelectedItems.Count != 0)
              {
                  string file = lstFiles.SelectedItems\[0\].Text;
                  openFormGeneral(file);
              }
          }
      

      In WPF .xaml:

      I Populate the ListView as follows:

          public class songDetails
          {
          public string Title { get; set; }
          public string Artist { get; set; }
          public string Track { get; set; }
          public string Set { get; set; }
          public string Album { get; set; }
          public string Genre { get; set; }
          public string Language { get; set; }
          public string FileName { get; set; }
          public string FilePath { get; set; }
          }
      
          public IList songDetail { get; set; }
      
      
          public void AddFile(ID3Info File)
      
      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      You bound the SelectedSong so SelecedSong.FilePath should contain your information provided you double clicked on a row so test for null!

      Never underestimate the power of human stupidity RAH

      M 1 Reply Last reply
      0
      • M Mycroft Holmes

        You bound the SelectedSong so SelecedSong.FilePath should contain your information provided you double clicked on a row so test for null!

        Never underestimate the power of human stupidity RAH

        M Offline
        M Offline
        MumbleB
        wrote on last edited by
        #3

        Hi Mycroft. Thanks for the reply. This may sound stupid but how do I do that? I have tried numerous things and nothing works or I get a very long string of all the items in the object returned. :(

        Excellence is doing ordinary things extraordinarily well.

        M 1 Reply Last reply
        0
        • M MumbleB

          Hi Mycroft. Thanks for the reply. This may sound stupid but how do I do that? I have tried numerous things and nothing works or I get a very long string of all the items in the object returned. :(

          Excellence is doing ordinary things extraordinarily well.

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          Wherever this property (SelectedSong) is declared is where you object is

          SelectedItem="{Binding Path=SelectedSong}"

          In your doubleclick event you need to reference the SelectedSong

          string sPath = class.SelectedSong.FileName

          If you are using MVVM then class = your viewmodel.

          Never underestimate the power of human stupidity RAH

          M 1 Reply Last reply
          0
          • M Mycroft Holmes

            Wherever this property (SelectedSong) is declared is where you object is

            SelectedItem="{Binding Path=SelectedSong}"

            In your doubleclick event you need to reference the SelectedSong

            string sPath = class.SelectedSong.FileName

            If you are using MVVM then class = your viewmodel.

            Never underestimate the power of human stupidity RAH

            M Offline
            M Offline
            MumbleB
            wrote on last edited by
            #5

            Hi Mycroft. Apologies for taking this long to reply. Been a little busy. I tried the below and I get the SelectedItem but it does not get passed to "theSong". Am I going crazy or am I just missing the point completely here?

                private void lstFile\_MouseDoubleClick(object sender, MouseButtonEventArgs e)
                {
                    try
                    {
                        var theSong = this.lstFile.SelectedItem as songDetails;
            
                        if (theSong != null)
                        {
                            string filePath = theSong.FilePath;
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message.ToString());
                    }
                    
                }
            

            Excellence is doing ordinary things extraordinarily well.

            M 1 Reply Last reply
            0
            • M MumbleB

              Hi Mycroft. Apologies for taking this long to reply. Been a little busy. I tried the below and I get the SelectedItem but it does not get passed to "theSong". Am I going crazy or am I just missing the point completely here?

                  private void lstFile\_MouseDoubleClick(object sender, MouseButtonEventArgs e)
                  {
                      try
                      {
                          var theSong = this.lstFile.SelectedItem as songDetails;
              
                          if (theSong != null)
                          {
                              string filePath = theSong.FilePath;
                          }
                      }
                      catch (Exception ex)
                      {
                          System.Windows.Forms.MessageBox.Show(ex.Message.ToString());
                      }
                      
                  }
              

              Excellence is doing ordinary things extraordinarily well.

              M Offline
              M Offline
              Mycroft Holmes
              wrote on last edited by
              #6

              You are missing the point completely :) you are thinking like winforms, stop. You should not/never be referencing the controls in the view but the collection it is bound to in you viewmodel or code behind. Stop trying to get the info from the list control and look in the SelectedSong property where you have bound the SelectedItem to!

              Never underestimate the power of human stupidity RAH

              M 1 Reply Last reply
              0
              • M Mycroft Holmes

                You are missing the point completely :) you are thinking like winforms, stop. You should not/never be referencing the controls in the view but the collection it is bound to in you viewmodel or code behind. Stop trying to get the info from the list control and look in the SelectedSong property where you have bound the SelectedItem to!

                Never underestimate the power of human stupidity RAH

                M Offline
                M Offline
                MumbleB
                wrote on last edited by
                #7

                I bound the SelectedSong to a textBox and it works now. Not the cleanest way of doing things but it works. Thanks

                Excellence is doing ordinary things extraordinarily well.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups