0

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
enter image description here

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
outlook email
  • 361
  • 1
  • 3
  • 14

1 Answers1

1

It looks like you have two instances of FolderBrowserDialogVM. One instance is DataContext for your FolderBrowserDialogV(here you have bound the command, but not the ObservableCollection) and another one for your FilesView(here you have bound ObservableCollection, but not the command, so your ObservableCollection will not be changed in this instance). For it works in your case, you should use(bind to) the same instance.

Delete from your UserControls:

<UserControl.DataContext>
    <vm:FolderBrowserDialogVM>
    </vm:FolderBrowserDialogVM>
</UserControl.DataContext>

Create an instance of your ViewModel in resources of your main window and set this instance as DataContext for your Usercontrols:

<MainWindow
xmlns:vm="clr-namespace:Bates_Writer.ViewModel" .. >

<MainWindow.Resources>
<vm:FolderBrowserDialogVM x:Key="vmInstance"/>
</MainWindow.Resources>

<StackPanel>

<YourUserControl1 DataContext="{StaticResource vmInstance}"/>
<YourUserControl2 DataContext="{StaticResource vmInstance}"/>

</StackPanel>

</MainWindow>
Rekshino
  • 6,954
  • 2
  • 19
  • 44
  • Without the detailed instructions like the things i need to delete from my user control and the codes i need to add in my mainwindow i wouldn't understand it. Thank you so much, you are a life saver. i almost give up in this mvvm thing. i've been coding using code behind. i am new to wpf. and i learn from doing things, not by reading, thank you – outlook email May 11 '17 at 11:02