2

I am trying to convert my existing program in c# wpf, using mvvm pattern.

The first part is select the Folder location of the files to be process and populate the listbox

I found an example here using Mvvm Light: WPF OpenFileDialog with the MVVM pattern?

the example in the link above is selecting a Folder.

this is the structure of my project
enter image description here

this is the code of my FileListView.xaml

<UserControl x:Class="MvvmLight1.Views.FilesListView"
             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:MvvmLight1.Views"
             mc:Ignorable="d" 
             d:DesignHeight="300" Width="730.029">

    <Grid>
        <ListBox ItemsSource="{Binding FileNames}" Margin="5,5,5,5"/>

    </Grid>
</UserControl>

this is my list which reside in ViewModel\OpenFileDialogVM.cs

public System.Collections.ObjectModel.ObservableCollection<string> FileNames { get; }
    = new System.Collections.ObjectModel.ObservableCollection<string>();

this is my code for populating the list. but it doesn't work

var files = System.IO.Directory.EnumerateFiles(SelectedPath, "*", System.IO.SearchOption.AllDirectories);

            FileNames.Clear();

            foreach (var file in files)
            {
                FileNames.Add(file);
                Console.WriteLine(file);
            }

What is wrong with my code above?


Code Update:

On my folder structure I have ViewModel Folder and inside it I have OpenFileDialogVm.cssenter image description here

but why is it that the IDE only recognize the ViewModelLocator.

I even Build the project.

I even set the DataContext in the CodeBehind of FileListView user control but still it doesn't populate the listbox

public partial class FilesListView : UserControl
    {
        public FilesListView()
        {
            DataContext = new OpenFileDialogVM();
            InitializeComponent();

        }
    }
Cœur
  • 37,241
  • 25
  • 195
  • 267
outlook email
  • 361
  • 1
  • 3
  • 14
  • 2
    Where do you set the DataContext for the FilesListView? – Rekshino May 10 '17 at 06:38
  • What does not work? Does the files variable not get populated or ListBox doesn't get populated? – sachin May 10 '17 at 06:44
  • @Rekshino i didn't set it. Where do i need to set it?. And i didn't even know that i need to set it. Thank you – outlook email May 10 '17 at 06:46
  • @sachin the variable is populated. i write the file names in the console using the for loop and its working – outlook email May 10 '17 at 06:47
  • http://stackoverflow.com/questions/606803/silverlight-setting-datacontext-in-xaml-rather-than-in-constructor – Rekshino May 10 '17 at 06:58
  • http://stackoverflow.com/questions/26206612/set-datacontext-in-xaml-rather-than-in-code-behind?rq=1 – Rekshino May 10 '17 at 07:00
  • Nice question to RelativeSource bindings: http://stackoverflow.com/questions/84278/how-do-i-use-wpf-bindings-with-relativesource?rq=1 – Rekshino May 10 '17 at 07:12
  • 1
    You need to learn how the DataContext works in WPF. It flows through the visual tree (which is why you NEVER want to bind DataContext to the visual tree itself `{Binding RelativeSource={RelativeSource Self}}`) from the root (typically the Window). But if it isn't set, your bindings won't work because there's nothing to bind against. You can use a tool like Snoop to examine the DataContext and your bindings at runtime. –  May 10 '17 at 14:56

1 Answers1

2

Add it to your UserControl:

<UserControl
.....
xmlns:viemodels="clr-namespace:MvvmLight1.ViewModels"
/>
    <UserControl.DataContext>
        <viemodels:OpenFileDialogVM/>
    </UserControl.DataContext>
....
</UserControl>
Rekshino
  • 6,954
  • 2
  • 19
  • 44
  • i tried your code but it doesn't work. the IDE doesn't recognize the viemodels:OpenFileDialogVM. i even set the data content in my code behind. but still it doesn't populate the listbox. thank you – outlook email May 11 '17 at 00:31
  • I don't know exactly what is the Name of your ViewModel class and what namespace it is into. The class must have public modifier and your ViewModel project must be referenced in View project. – Rekshino May 11 '17 at 06:06