在WPF应用程序中用TreeView和ListView显示文件夹和文件,添加一个Button(contentpropertyisListChange),点击Button后把在ListView中所选的文件再在该文件夹下生成一个,扩展名为.abc。问题是添加文件后在ListView中不能立即显示。
具体代码如下:(本代码为测试ListView更新,并非完整代码,代码编译后可运行)
XAML代码:
[Tr]
[Tr]
[/Tr]
[/Tr]
[B]ListChange[/B]
CS代码如下:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows;
usingSystem.Windows.Controls;
usingSystem.Windows.Data;
usingSystem.Windows.Documents;
usingSystem.Windows.Input;
usingSystem.Windows.Media;
usingSystem.Windows.Media.Imaging;
usingSystem.Windows.Navigation;
usingSystem.Windows.Shapes;
////////////////////////////////
usingSystem.Collections.ObjectModel;
usingSystem.IO;
usingSystem.ComponentModel;//forINotifyPropertyChanged
//usingSystem.Windows.Forms;
namespaceTestTreeViewAndDataView
{
///
///Window1.xaml的交互逻辑
///
publicpartialclassWindow1:Window
{
privateObservableCollectionfileInformation=newObservableCollection();
publicWindow1()
{
InitializeComponent();
Directory_Load();
LoadDetailView();
}
privatevoidDirectory_Load()
{
vardirectory=newObservableCollection();
foreach(vardriveinDriveInfo.GetDrives())
{
directory.Add(
newDirectoryRecord
{
Info=newDirectoryInfo(drive.RootDirectory.FullName)
}
);
}
directoryTreeView.ItemsSource=directory;
}
privatevoidLoadDetailView()
{
ListrequiredProperties=newList
{
"Name","Length","IsReadOnly","LastWriteTime"
};
foreach(stringnameinrequiredProperties)
{
GridViewColumncolmun=newGridViewColumn{Header=name};
Bindingbinding=newBinding(name);
colmun.DisplayMemberBinding=binding;
leftDetailView.Columns.Add(colmun);
}
}
privatevoidbutton1_Click(objectsender,RoutedEventArgse)
{
//OnlyfortestListViewupdate,notcheckwhetherfileInformationcollectionempt!!!!
foreach(FileInfofileInfoinfileInformation)
{
stringcreateFilePath=fileInfo.FullName+".abc";
//Notdeterminewhetherthefileexsist.OnlyfortestListViewupdate.
using(StreamReadersr=newStreamReader(fileInfo.FullName))
{
using(StreamWritersw=newStreamWriter(createFilePath))
{
while(sr.Peek()>=0)
sw.Write((char)sr.Read());
}
}
}
}
privatevoidlistView_SelectionChanged(objectsender,SelectionChangedEventArgse)
{
//formultypleselectionofListViewitems
if((Keyboard.Modifiers&ModifierKeys.Control)>0)
{
System.Collections.IListfi=this.listView.SelectedItems;
fileInformation.Clear();
foreach(FileInfoinforinfi)
fileInformation.Add(infor);
}
else
{
fileInformation.Clear();
fileInformation.Add((FileInfo)listView.SelectedItem);
}
}
}
classDirectoryRecord:INotifyPropertyChanged
{
privateDirectoryInfoinfo;
publicDirectoryInfoInfo
{
get
{
returnthis.info;
}
set
{
this.info=value;
OnPropertyChanged(newPropertyChangedEventArgs("Info"));
}
}
publicIEnumerableFiles
{
get
{
returnInfo.GetFiles();
}
}
publicIEnumerableDirectories
{
get
{
try
{
returnfromdiinInfo.GetDirectories("*",SearchOption.TopDirectoryOnly)
selectnewDirectoryRecord{Info=di};
}
catch
{
returnnull;
}
}
}
publicvoidOnPropertyChanged(PropertyChangedEventArgse)
{
if(PropertyChanged!=null)
PropertyChanged(this,e);
}
#regionINotifyPropertyChanged成员
publiceventPropertyChangedEventHandlerPropertyChanged;
#endregion
}
}
分 -->
|