[英]IValueConverter and convert all values of bindinglist
我有一个带有INotifyPropertyChanged接口的绑定列表。 一切正常。 此绑定列表是绑定到列表框的文件名列表。 我只希望显示名称,而不是整个路径,但是当我选择文件名并加载文件时,我需要整个路径。
我为此使用IValueConverter,其中使用Path.GetFileName属性更改文件名的完整路径。 绑定是正确的,但是我的绑定列表没有按我希望的那样更改值。 我在下面粘贴IValueConverter代码。 请让我知道此代码有什么问题。 我在这里提到了转换。
[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;
}
}
编辑:现在转换值。 但是现在如何找回整个路径? 我可以保留2个清单吗? 一个完整路径和一个与像名字在这里 。 有更好的解决方案吗?
要返回文件名和FullPath,您必须创建一个新类:
public class MyFile
{
public string Filename { get; set; }
public string Fullpath { get; set; }
}
之后,您必须在转换器中返回MyFile列表
[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;
}
}
最后,在列表框中,可以使用MyFile的DataBinding属性进行检索
希望对您有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.