0

I have a bindinglist with INotifyPropertyChanged interface. Things work fine. This bindinglist is a list of file names that is bound to a listbox. I want only the name displayed, not the whole path, but when I select a file name and load the file, I need the whole path.

I am using IValueConverter for this purpose where is use Path.GetFileName property to change full path to file name. Bindings are correct, but my bindinglist is not changing values as I want them to. I am pasting IValueConverter code below. Please let me know what is wrong with this code. i referred conversion here.

[ValueConversion(typeof(string), typeof(string))]
public class pathtoname : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter ,CultureInfo culture)
    {
        BindingList<string> ls = value as BindingList<string>;
        ls.Select(x => "WW");
        return ls;//all values should be WW. But they are not.(debugger)
    }

    public object ConvertBack(object value, Type targetType, object parameter,CultureInfo culture)
    {
        return null;
    }

}

EDIT: Values are converted now. But now how to retrieve back the whole path? Shall I keep 2 lists? One for full path and one with name like here. Any better solution?

Community
  • 1
  • 1
Naresh
  • 633
  • 1
  • 7
  • 26

1 Answers1

1

To return File name & FullPath, you have to create a new class :

public class MyFile
{
    public string Filename { get; set; }
    public string Fullpath { get; set; }
}

After, you have to return a List of MyFile in your converter

[ValueConversion(typeof(string), typeof(string))]
public class pathtoname : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter ,CultureInfo culture)
    {
        BindingList<string> ls = value as BindingList<string>;
        List<MyFile> files = new List<MyFile>();
        foreach (string s in ls)
        {
            files.Add(new MyFile() { Filename = Path.GetFileName(s), Fullpath = s });
        }

        return files;
    }

    public object ConvertBack(object value, Type targetType, object parameter,CultureInfo culture)
    {
        return null;
    }
}

Finally, in your Listbox, you can retrieve with DataBinding properties of MyFile

Hope it's help !

Joffrey Kern
  • 6,449
  • 3
  • 25
  • 25
  • ok. that worked. but i got another prob now. my loading files lost full path. tips on how to keep full path but display only the file names? – Naresh Jul 01 '13 at 14:47
  • To get the Filename, you can try to look on GetFileName method from Path static class : http://msdn.microsoft.com/en-us/library/system.io.path.getfilename.aspx – Joffrey Kern Jul 01 '13 at 14:49
  • ah, i know that. from file name, i want to go to a full path so i can pass the file through another function. but the converter lost the information. – Naresh Jul 01 '13 at 14:50
  • This converter is use with a ComboBox ? – Joffrey Kern Jul 01 '13 at 14:53
  • listbox. values are displayed in a listbox. i click on file name, that should load the file from hdd. – Naresh Jul 01 '13 at 14:54
  • that should work. though i have full names already so I dont have to exactly follow this code. Its enough for me to know that i have make a new list. thanks a lot. – Naresh Jul 01 '13 at 15:17
  • In your ItemTemplate, you have to use databinding. Have a look on this article : http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemtemplate.aspx – Joffrey Kern Jul 01 '13 at 15:26