我建立一個文件瀏覽器,我有一個綁定到一個ObservableCollection我在想什麼,當有人點擊該文件夾(樹形視圖左側)上一個ListView它填充列表視圖並在文本塊中填寫正確的文件信息。綁定XAML對SelectedItemChanged的ObservableCollection返回null
我發現this幫助我到達我的所在地。但我仍然在文本塊中返回空值。 感謝您的幫助!
我有一個private string start_Path
代碼來填充ListView控件:
private void load_ListView(string path)
{
var lv = File_List;
lv.Items.Clear();
var search_Directory = new DirectoryInfo(path);
var item = new ListViewItem();
try
{
foreach (var file in search_Directory.GetFiles())
{
lv.Items.Add(file);
}
}
catch (Exception ex)
{
}
}
private void frm_File_Directory_Loaded(object sender, RoutedEventArgs e)
{
ListDirectory(foldersItem, start_Path.ToString());
}
private void foldersItem_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
load_ListView(start_Path + "\\" + ((TreeViewItem)e.NewValue).Header.ToString());
folder_Name = ((TreeViewItem)e.NewValue).Header.ToString();
this.DataContext = File_Info_data.get_Files(start_Path + "\\" + ((TreeViewItem)e.NewValue).Header.ToString());
}
的ObservableCollection:
public static ObservableCollection<File_Information> get_Files(string path)
{
var temp = new ObservableCollection<File_Information>();
File_Information file;
FileInfo fileInfo = new FileInfo(path);
try
{
file = new File_Information
{
file_Size = fileInfo.Length,
date_Modified = fileInfo.LastWriteTime,
file_Type = get_File_Type(fileInfo.Extension)
};
temp.Add(file);
return temp;
}
catch (Exception ex) { }
return null;
}
public static string get_File_Type(string extension)
{
string ext_Name = null;
switch (extension)
{
case @"xlsx":
case "xlsm":
case "xls":
ext_Name = "Excel File";
break;
case "docx":
case "docm":
case "doc":
ext_Name = "Word Document";
break;
case "pdf":
ext_Name = "PDF Document";
break;
case "cad":
ext_Name = "CAD File";
break;
case "DWG":
ext_Name = "AutoCAD Drawing";
break;
case "jpg":
ext_Name = "JPEG image";
break;
default:
ext_Name = "Unknown File Type";
break;
}
return ext_Name;
}
XAML:
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Converter={StaticResource PathConverter}}"
Height="20"
Width="20"
Stretch="UniformToFill"
/>
<TextBlock x:Name="file_Name" Text="{Binding}" Width="300"></TextBlock>
<TextBlock x:Name="Date_Modified" Text="{Binding date_Modified}" Width="200"></TextBlock>
<TextBlock x:Name="File_Type" Text="{Binding file_Type}" Width="150"></TextBlock>
<TextBlock x:Name="File_Size" Text="{Binding file_Size}" Width="150"></TextBlock>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
什麼,在哪裏回報'null'? – Sinatr
View-model中的收藏屬性在哪裏? –
我的文件名顯示出來,剩下的就是空 – user3120232