Databinding issue

Page 1 of 1

5 Replies - 3653 Views - Last Post: 11 April 2012 - 03:32 PM Rate Topic: -----

#1 BigR1983  Icon User is offline

  • D.I.C Head

Reputation: 57
  • View blog
  • Posts: 221
  • Joined: 12-April 10

Databinding issue

Posted 10 April 2012 - 05:51 PM

So i am creating a recipe app, and am following a MVVM tutorial, and I have run into an issue.
I have a listbox that gets a list of recipes from a repository class, and when i select a recipe from the listbox, i want to populate a usercontrol with the details. My issue is in populating the usercontrol. I have a property ActiveRecipe
public Recipe ActiveRecipe 
{ 
    get 
    {
        return _activeRecipe; 
    }
    set 
    {
        _activeRecipe = value;
        RaisePropertyChanged("ActiveRecipe");
    } 
}



When an item is selected from the listbox, I call
public RecipeSearchResults SelectedRecipe
{
     get
     {
         return _selectedRecipe;
     }
     set
     {
         _selectedRecipe = value;
         RaisePropertyChanged("SelectedRecipe");
         RecordSelector(value);
     }
}

private void RecordSelector(RecipeSearchResults value)
{
      if (value == null) return;
      try
      {
          this.ActiveRecipe = _recipeRepository.GetById(value.Id);
      }
      catch (Exception)
      {
      }
}



I am binding in the control in the following manner
<TextBox Grid.Column="1" Grid.Row="0" Text="{Binding Path=ActiveRecipe.Name, Mode=TwoWay}" />



Is there something obvious that I am missing?
It seems like it should be working for me, but when I step through it, I never hit the get in the ActiveRecipe property.

Is This A Good Question/Topic? 0
  • +

Replies To: Databinding issue

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Databinding issue

Posted 11 April 2012 - 06:50 AM

Maybe I am missing it, but why do you have one object that populates the ListBox, but another object that you try to get from the information from the object in the ListBox? Why isn't it just one class, Recipe? Why have both Recipe and RecipeSearchResults. It would seem that the ListBox should be populated with a List<Recipe>, or ObservableCollection<Recipe>, or some other type of generic collection, and when the user selects from the ListBox, you simply change the ActiveRecipe in the ViewModel.

We need to see more XAML. Where are you setting the DataContext for the TextBox to get the information?
Was This Post Helpful? 1
  • +
  • -

#3 BigR1983  Icon User is offline

  • D.I.C Head

Reputation: 57
  • View blog
  • Posts: 221
  • Joined: 12-April 10

Re: Databinding issue

Posted 11 April 2012 - 07:32 AM

I was following an example that created a new object type for the items in the listbox. I think i know how I can rework it to use just the Recipe class.

The data context is set in the mainwindow.xaml file
<window.DataContext>
        <local:MainWindowViewModel />
</window.DataContext>



and i add my controls to this xaml file
<local:SearchView Grid.Row="1" Margin="6,6,3,6" />       
<local:DetailView Grid.Column="1" Grid.Row="1" Margin="3,6,6,6" />


Was This Post Helpful? 0
  • +
  • -

#4 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: Databinding issue

Posted 11 April 2012 - 09:21 AM

Is the binding in your User Control depending on the DataContext of the user control?
If so, in the ActiveRecipe property's set block, set this.DataContext=_activeRecipe or value.

If the bindings of the controls in your User Control are not depending on the DataContext, then you will have to provide more code snippets for us to see how you are binding the controls in your User Control to the Recipe.


Aside: If you want to bind the user control to the selected recipe in the list, you have to change your ActiveRecipe property into a dependency property. You would have to implement a method that handles when the dependency property changes to set the DataContext of the user control. Or you need to change your bindings in the User Control to access the User Control's AcitveRecipe property.

This post has been edited by Frinavale: 11 April 2012 - 11:00 AM

Was This Post Helpful? 1
  • +
  • -

#5 BigR1983  Icon User is offline

  • D.I.C Head

Reputation: 57
  • View blog
  • Posts: 221
  • Joined: 12-April 10

Re: Databinding issue

Posted 11 April 2012 - 10:27 AM

Thanks for the tips, I will try them out tonight when I am home, and see what happens.
Was This Post Helpful? 0
  • +
  • -

#6 BigR1983  Icon User is offline

  • D.I.C Head

Reputation: 57
  • View blog
  • Posts: 221
  • Joined: 12-April 10

Re: Databinding issue

Posted 11 April 2012 - 03:32 PM

Changing the to have the list use the recipe class, and everything seems to be working now.

Thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1