Reputation: 643
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?
Upvotes: 0
Views: 1174
Reputation: 6499
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 !
Upvotes: 1