2021-07-04

桌面应用开发的日子(二):实现文件资源管理列表树加载

已矣乎!寓形宇内复几时?曷不委心任去留?胡为乎遑遑欲何之?富贵非吾愿,帝乡不可期。怀良辰以孤往,或植杖而耘耔。登东皋以舒啸,临清流而赋诗。聊乘化以归尽,乐夫天命复奚疑!————《归去来兮辞并序》·陶渊明

实现效果

获取本地逻辑驱动器、文件夹、文件

Directory.GetLogicalDrives();Directory.GetDirectories(fullPath); // 返回指定目录中的子目录Directory.GetFiles(fullPath); // 返回指定目录中的文件// 截取文件或文件夹名称public static string GetFileFolderName(string path){ if (string.IsNullOrEmpty(path))  return string.Empty; path = path.Replace("/", "\\"); var lastIndex = path.LastIndexOf("\\"); if (lastIndex <= 0)  return path; return path.Substring(lastIndex + 1);}

通过mvvm模式,实现后台数据与ui进行绑定,下面是树UI结构定义和实体,视图模型的定义

<TreeView x:Name="FolderTreeView" Grid.Row="1" ItemsSource="{Binding Items}"> <TreeView.ItemContainerStyle>  <Style TargetType="{x:Type TreeViewItem}">   <Setter Property="IsExpanded" Value="{Binding IsExpanded,Mode=TwoWay}"/>  </Style> </TreeView.ItemContainerStyle> <TreeView.ItemTemplate>  <HierarchicalDataTemplate ItemsSource="{Binding Children}">   <StackPanel Orientation="Horizontal">    <TextBlock Margin="3" FontFamily="{StaticResource IconFont}" FontSize="26" Foreground="{Binding IconColor}" Text="{Binding Icon}" />    <TextBlock VerticalAlignment="Center" FontSize="18" Text="{Binding Name}"/>   </StackPanel>  </HierarchicalDataTemplate> </TreeView.ItemTemplate></TreeView>
public class DirectoryItem{ public string FullPath { get; set; } public DirectoryItemType Type { get; set; } public string Name {  get  {   return this.Type == DirectoryItemType.Drive ?    this.FullPath :    DirectoryStructure.GetFileFolderName(this.FullPath);  } } public string Icon { get; set; } public Brush IconColor { get; set; }}
public class DirectoryItemViewModel : BaseViewModel{ /// <summary> /// 全路径 /// </summary> public string FullPath { get; set; } /// <summary> /// 成员类型 /// </summary> public DirectoryItemType Type { get; set; } /// <summary> /// 成员名称 /// </summary> public string Name {  get  {   return this.Type == DirectoryItemType.Drive ?    this.FullPath :    DirectoryStructure.GetFileFolderName(this.FullPath);  } } /// <summary> /// 成员图标 /// </summary> public string Icon { get; set; } /// <summary> /// 图标颜色 /// </summary> public Brush IconColor { get; set; } /// <summary> /// 子成员 /// </summary> public ObservableCollection<DirectoryItemViewModel> Children { get; set; } /// <summary> /// 表示成员是否可以展开 /// </summary> public bool CanExpend { get { return this.Type != DirectoryItemType.File; } } /// <summary> /// 展开/收缩的处理 /// </summary> public bool IsExpanded {  get  {   return this.Children?.Count(x => x != null) > 0;  }  set  {   if (value)    Expand();   else    ClearChildren();  } } public ICommand ExpandCommand { get; set; } public DirectoryItemViewModel() {  this.ExpandCommand = new RelayCommand(Expand);  this.ClearChildren(); } private void ClearChildren() {  this.Children = new ObservableCollection<DirectoryItemViewModel>();  if (this.Type != DirectoryItemType.File)   this.Children.Add(null); } /// <summary> /// 展开目录并找到所有子成员 /// </summary> private void Expand() {  if (this.Type == DirectoryItemType.File)   return;  var children = DirectoryStructure.GetDirectContents(this.FullPath);  var selector = children.Select(x => new DirectoryItemViewModel()  {   FullPath = x.FullPath,   Type = x.Type,   Icon = x.Icon,   IconColor = x.IconColor  });  this.Children = new ObservableCollection<DirectoryItemViewModel>(selector); }}public class DirectoryStructureViewModel : BaseViewModel{ public ObservableCollection<DirectoryItemViewModel> Items { get; set; } public DirectoryStruct......

原文转载:http://www.shaoqun.com/a/846140.html

跨境电商:https://www.ikjzd.com/

吴佳:https://www.ikjzd.com/w/1770

黄劲:https://www.ikjzd.com/w/2426

paipaiwang:https://www.ikjzd.com/w/2205


已矣乎!寓形宇内复几时?曷不委心任去留?胡为乎遑遑欲何之?富贵非吾愿,帝乡不可期。怀良辰以孤往,或植杖而耘耔。登东皋以舒啸,临清流而赋诗。聊乘化以归尽,乐夫天命复奚疑!————《归去来兮辞并序》·陶渊明实现效果获取本地逻辑驱动器、文件夹、文件Directory.GetLogicalDrives();Directory.GetDirectories(fullPath);//返回指定目录中的
淘粉吧怎么返利:https://www.ikjzd.com/w/1725
情人节、超级碗、樱桃月···二月份这么多节卖家做好逆袭的准备了吗!:https://www.ikjzd.com/articles/15739
春运进行时 -正在火车上的你,是否有回老家二三线城市做跨境电商的打算?:https://www.ikjzd.com/articles/15742
亚马逊仓库失火后,这项新技术正在推行:https://www.ikjzd.com/articles/15744
如何打造一个优秀的listing来带动产品的销量和转化率?:https://www.ikjzd.com/articles/15747
换妻游戏没能给我带来难忘的快感(3/3):http://lady.shaoqun.com/a/66904.html
小玉面试体检被教授摸底全文阅读 应聘时医生慢慢探入:http://lady.shaoqun.com/a/246886.html
把车开到没人的地方做 男朋友在车里㖭我高潮:http://lady.shaoqun.com/m/a/247148.html
为什么女朋友总是抱你?一个正常的生理需求,心理就会得到满足:http://lady.shaoqun.com/a/401715.html
被女票坐是什么感觉?:http://lady.shaoqun.com/a/401716.html
半夜在公园里甜蜜的邂逅,很难克服利用它失去你的心的温暖:http://lady.shaoqun.com/a/401717.html
如果一个女人很孤独,那么她在完成这三件事之后,就再也不会感到孤独了:http://lady.shaoqun.com/a/401718.html

No comments:

Post a Comment