Good Afternoon,
I have a project that launches from command line. If the command line argument specifies it, then my project will launch with a list view representing a directory browse of the requested folder.
I started out binding my listview control to a "DirectoryItems" class with two properties one for an Icon to represent a file or folder and the other representing the name of the folder or file. I bound my listview datacontext to the class itself and in a xaml template, I bound each field of that class.
When I loaded my project, I just had a sub that looped through the objects in the directory, created new listviewitems and added them to the listview. It loaded great. However, now I have added the ability to drill down to a sub directory with a double click...aaaannnndddd thats where the problems started to begin.
So now I am trying to bind my listview to an Observable Collection (cause people in blogs say to do it this way) and then load the collection and implement a property changed event so that I can clear my listview properly and load new objects.
So here goes:
I have a class called "DirectoryItems" with only two properties (I am probably going to add more later) and a method that I call from another class called "BindingListItems" That has the Observable Collection and a method to load the collection.
DirectoryItems
BindingListItemsCode:Imports System.Collections.ObjectModel Public Class DirectoryItems Public Property ImageSrc As String Public Property DirName As String Public Sub New(imageSource As String, name As String) Me.ImageSrc = ImageSrc Me.DirName = DirName End Sub End Class
So what I was hoping for was that I could use BindingListItems as a single class to bind to then if I wanted to create some other class called "FileItems" or something, I could make it and just add code in BindingListItems and not need to change my XAML Binding. However, if I am binding to the Observable Collection, how the hell do I bind/retrieve the specific properties that I added to the Collection (the icon source and the name of the folder/file) to my listviewitem template with Binding?Code:Imports System.Collections.ObjectModel Imports System.ComponentModel Public Class BindingListItems Implements INotifyPropertyChanged Public Property DirCol As ObservableCollection(Of DirectoryItems) Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged Public Sub New(path As String) DirCol = New ObservableCollection(Of DirectoryItems)() For Each dirInfo As String In IO.Directory.GetDirectories(path) DirCol.Add(New DirectoryItems(dirInfo.Substring(dirInfo.LastIndexOf("\") + 1), New Uri("Resources/Folder.ico", UriKind.Relative).ToString)) Next For Each fileInfo As String In IO.Directory.GetFiles(path) DirCol.Add(New DirectoryItems(fileInfo.Substring(fileInfo.LastIndexOf("\") + 1), New Uri("Resources/Folder.ico", UriKind.Relative).ToString)) Next End Sub End Class
XAML
Basically what happens now is my project loads but the listview is blank. When I debug, I can see that the collection is being loaded properly. Any help would be greatly appreciated. If I need to change a bunch of stuff around to make this work, I am totally good with that...I am more just trying to understand how binding works.Code:<ListView x:Name="dirViewer" ItemsSource="{Binding BindingListItems.DirCol}" SelectionChanged="dirViewer_SelectionChanged" MouseDoubleClick="dirViewer_MouseDoubleClick" HorizontalAlignment="Stretch" Height="auto" Grid.Row="1" VerticalAlignment="Stretch" Width="auto"> <ListView.View> <GridView/> </ListView.View> <ListView.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Image Source="{Binding ImageSrc}"/> <TextBlock Text="{Binding DirName}"/> </StackPanel> </DataTemplate> </ListView.ItemTemplate> </ListView>
Like I have seen examples where in MainWindow initialization you set the datacontext of the listview in codebehind. Is this mandatory or can I just do the Binding all in XAML? Right like if I have to instantiate my class in code behind initialization, kind of defeats the purpose of binding in XAML doesnt it? or no?
Thank you for your time.




Reply With Quote
