WPF应用程序中ListView添加文件后更新问题

[复制链接]
查看11 | 回复3 | 2021-1-27 05:57:28 | 显示全部楼层 |阅读模式
在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
}

}

分 -->
回复

使用道具 举报

千问 | 2021-1-27 05:57:28 | 显示全部楼层
没有缩进,再贴





[Tr]
[Tr]


[/Tr]
[/Tr]





[B]ListChange[/B]




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);
}
}
//OnlyfortestListViewupdate,notcheckwhetherfileInformationcollectionisempty!!!!
privatevoidbutton1_Click(objectsender,RoutedEventArgse)
{
foreach(FileInfofileInfoinfileInformation)
{
//Notdeterminewhetherthefileexsist.OnlyfortestListViewupdate.
stringcreateFilePath=fileInfo.FullName+".abc";
using(StreamReadersr=newStreamReader(fileInfo.FullName))
{
using(StreamWritersw=newStreamWriter(createFilePath))
{
while(sr.Peek()>=0)
sw.Write((char)sr.Read());
}
}
//MessageBox.Show(this.directoryTreeView.Items.ToString());
//DirectoryRecorddr=newDirectoryRecord();
//dr.Info=fileInfo.Directory;
}
}
privatevoidlistView_SelectionChanged(objectsender,SelectionChangedEventArgse)
{
//formultypleselectionsofListViewitems
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
}

}

回复

使用道具 举报

千问 | 2021-1-27 05:57:28 | 显示全部楼层
这个问题很难回答呀,关键是Directory_Load()之后邦定的数据已经固定了,我尝试重新调用这个函数,但调用之后ListView中已经不是所选的那个文件夹了。尝试使用(TreeViewItem)directoryTreeView.ItemContainerGenerator.ContainerFromItem(directoryTreeView.SelectedItem),则除根结点之外一律返回null。
回复

使用道具 举报

千问 | 2021-1-27 05:57:28 | 显示全部楼层
从SelectedItem可能等到TreeView选择的结点,但在TreeView中SelectedItem是只读属性,不能设置。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行