[英]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.