I have asked how to bind listbox to mvvm in this thread: Listbox is not Populating using binding
I created another thread to ask why my ObservableList is not Refreshing.
The List is now populated on Load
but not populating or refreshing after I select the Folder
The program is supposed to Populate the list after I select the program
this is the structure of my folders and class
this is the code for my FolderBrowserDialogVM
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Bates_Writer.ViewModel
{
public class FolderBrowserDialogVM :ViewModelBase
{
public System.Collections.ObjectModel.ObservableCollection<string> FileNames { get; }
= new System.Collections.ObjectModel.ObservableCollection<string>()
{
"Wayne",
"Cinderella",
"Malificient"
};
public static RelayCommand OpenCommand { get; set; }
private string _selectedPath;
public string SelectedPath
{
get { return _selectedPath; }
set
{
_selectedPath = value;
RaisePropertyChanged("SelectedPath");
}
}
private string _defaultPath;
public FolderBrowserDialogVM()
{
RegisterCommands();
}
public FolderBrowserDialogVM(string defaultPath)
{
_defaultPath = defaultPath;
RegisterCommands();
}
private void RegisterCommands()
{
OpenCommand = new RelayCommand(ExecuteOpenFileDialog);
}
private void ExecuteOpenFileDialog()
{
var dialog = new FolderBrowserDialog();
dialog.ShowDialog();
SelectedPath = dialog.SelectedPath;
FileNames.Clear();
foreach (var file in System.IO.Directory.EnumerateFiles(SelectedPath, "*", System.IO.SearchOption.AllDirectories))
{
FileNames.Add(file);
Console.WriteLine(file);
}
}
}
}
this is the code for FilesView.xaml(user control)
<UserControl x:Class="Bates_Writer.View.FilesView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Bates_Writer.View"
xmlns:vm="clr-namespace:Bates_Writer.ViewModel"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.DataContext>
<vm:FolderBrowserDialogVM>
</vm:FolderBrowserDialogVM>
</UserControl.DataContext>
<Grid>
<ListBox ItemsSource="{Binding FileNames}" Margin="5,5,5,5"/>
</Grid>
</UserControl>
I tried How to: Implement Property Change Notification
but I can't make it work.
this is my FolderBrowserDialogV.xaml(user control)
<UserControl x:Class="Bates_Writer.View.FolderBrowserDialogV"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Bates_Writer.View"
xmlns:vm="clr-namespace:Bates_Writer.ViewModel"
mc:Ignorable="d" Height="42.056" Width="679.439">
<UserControl.DataContext>
<vm:FolderBrowserDialogVM>
</vm:FolderBrowserDialogVM>
</UserControl.DataContext>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="127*"/>
<ColumnDefinition Width="506*"/>
<ColumnDefinition Width="110*"/>
</Grid.ColumnDefinitions>
<Label Content="Folder Location: " FontSize="14" VerticalContentAlignment="Center"/>
<TextBox Grid.Column="1" FontSize="14" Margin="5,5,5,5" VerticalContentAlignment="Center" Text="{Binding SelectedPath}"/>
<Button Command="{Binding OpenCommand}" Content="Browse" Grid.Column="2" FontSize="14" Margin="5,5,5,5" Cursor="Hand"/>
</Grid>
</UserControl>
I even uploaded the sample project here