Get File Path from ListView
-
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)
-
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)
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
-
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
-
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.
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
-
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
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.
-
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.
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
-
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