Zu Hauptinhalt springen

 none
Dateibaum in der WPF RRS feed

  • Frage

  • Hallo,

    ich weiß, es gibt viele Tutorials im Internet, wie man in der U WP mit einer TreeView einen Dateibaum macht (ich weiß nicht wie es richtig heißt,  aber auf Englisch ist es "file tree view"), aber ich habe  keins verstanden. Nun frag ich euch.

    Also: Wie macht man das? 


    Dienstag, 19. November 2019 18:34

Antworten

  • Hi Dennis,
    hier mal eine Demo einer UWP-Anwendung für die Anzeige der Ordner und Dateien des Bilder-Ordners in einem TreeView.

    <Page
        x:Class="App1.Page01"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App1"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
      <Page.DataContext>
        <local:Page01VM/>
      </Page.DataContext>
      <Grid>
        <TreeView ItemsSource="{Binding View}">
          <TreeView.ItemTemplate>
            <DataTemplate x:DataType="local:Page01Data">
              <TreeViewItem ItemsSource="{x:Bind SubItems}" Content="{x:Bind Info}"/>
            </DataTemplate>
          </TreeView.ItemTemplate>
        </TreeView>
      </Grid>
    </Page>
    

    Und der ViewModel dazu:

    Imports System.Text
    Imports Windows.Storage
    
    Public Class Page01VM
    
      Implements INotifyPropertyChanged
    
      Public Sub New()
        LoadData()
      End Sub
    
      Private col As New ObservableCollection(Of Page01Data)
      Private cvs As New CollectionViewSource
    
      Public ReadOnly Property View As ICollectionView
        Get
          Return cvs.View
        End Get
      End Property
    
      Private Async Sub LoadData()
        Dim picturesFolder As StorageFolder = KnownFolders.PicturesLibrary
        Dim folders As IReadOnlyList(Of StorageFolder) = Await picturesFolder.GetFoldersAsync
        For Each folder As StorageFolder In folders
          Dim fo As New Page01Data With {.Info = folder.Name}
          col.Add(fo)
          Dim files1 As IReadOnlyList(Of StorageFile) = Await folder.GetFilesAsync
          For Each file In files1
            fo.SubItems.Add(New Page01Data With {.Info = file.Name})
          Next
        Next
        Dim files2 As IReadOnlyList(Of StorageFile) = Await picturesFolder.GetFilesAsync
        For Each file In files2
          col.Add(New Page01Data With {.Info = file.Name})
        Next
        cvs.Source = col
        OnPropChanged(NameOf(View))
      End Sub
    
      Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
      Private Sub OnPropChanged(<CallerMemberName> Optional propName As String = "")
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propName))
      End Sub
    
    End Class
    
    Public Class Page01Data
      Public Property Info As String
      Public Property SubItems As New ObservableCollection(Of Page01Data)
    End Class


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Mittwoch, 20. November 2019 08:20