1
votes

I get data from a DataLayer Class into the ViewModel but it doesn't get into the observableCollection that is binded with the listbox in the View.

public class ViewModel : NotifyUIBase
{
    public ViewModel()
    {
         FindImageCommand = new RelayCommand(FindImage);
    }

    public ObservableCollection<Image> FindVisualReferences { get; set; }       
    public RelayCommand FindImageCommand { get; private set; }

    private void FindImage()
    {
         string SearchTerm = this.SearchBox;
         var dbFunctions = new DatabaseFunctions();
         FindVisualReferences = dbFunctions.FindVisualReferences(SearchTerm);
    }
}

I've tested the FindVisualReferecences(SearchTerm) Method and it gives the items needed to fill the observableCollection. I also tested the databinding with the View and that's working to.

When I place the method in the constructor the ObservableCollection gets filled

  public ViewModel()
    {
         var dbFunctions = new DatabaseFunctions();
         FindVisualReferences = dbFunctions.FindVisualReferences(SearchTerm);
    }

But I need to call the method when the user is gives the FindImageCommand. How can I make this work? I'm really a rookie when it comes to coding.

1
what's the problem ? are you not executing FindImageCommand ? - eran otzap
How do you activate the command? through a button or something like that? - israel altar
The command executes and and I can get the data into the ViewModel, but somehow it doesn't reach the ObservableCollection in the ViewModel, although the FindVisualReferences in the FindImage Method holds the items that I need for the ObservableCollection to bind with the View. - Phil

1 Answers

0
votes

Your problem is that your setting your FindVisualReference property with a new instance of ObservableCollection instead of adding and removing from your existing one.

public ObservableCollection<Image> FindVisualReferences { get; set; }

private void FindImage()
{
     string SearchTerm = this.SearchBox;
     var dbFunctions = new DatabaseFunctions();
     FindVisualReferences.Clear();
     FindVisualReferences.AddRange(dbFunctions.FindVisualReferences(SearchTerm));
}

Alternatively you can do as you've done and set the property

FindVisualReferences = dbFunctions.FindVisualReferences(SearchTerm); 

but in order for this to work you would have to Raise the PropertyChanged event.

private ObservableCollection<Image> _findVisualReferences;
public ObservableCollection<Image> FindVisualReferences 
{ 
   get{ return _findVisualReferences;}
   set
   {
       _findVisualReferences = value;
       RaiseProperyChanged("FindVisualReferences");
   }
}

But if your doing that there's not much point in using an ObservableCollection to being with so just use List instead.

My suggestion , make it a readonly property (i.e. getter only) and add to it.

private ObservableCollection<Image> _findVisualReferences;
public ObservableCollection<Image> FindVisualReferences 
{ 
   get
   {
       if(_findVisualReferences == null)
          _findVisualReferences = new ObservableCollection<Image>(); 
       return _findVisualReferences;
   }       
}