.NET Framework Developer Center > .NET Development Forums > Windows Presentation Foundation (WPF) > Can you update the value of an ItemsSource item from the control?
Ask a questionAsk a question
 

AnswerCan you update the value of an ItemsSource item from the control?

  • Thursday, July 02, 2009 5:45 PMKofoed Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Say, for example, I have a ListBox.  The ListBox has it's ItemsSource bound to some data object behind the scenes.  Is it possible to change the actual value of a data item behind the scenes by changing it's associated bound ListBoxItem content/text?

    I would think that in theory two-way binding would work here ... but some preliminary tests seem to not be working.  Am I missing something, or is two way binding not applicable in this case?

Answers

All Replies

  • Thursday, July 02, 2009 5:48 PMMariano O. Rodriguez Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    You have to implement INotifyPropertyChanged in your  model object and raise NotifyPropertyChanged from the properties of your model. It is the way that WPF listen changes.
    http://weblogs.asp.net/marianor/
  • Thursday, July 02, 2009 6:02 PMKofoed Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Mariano - that's a good point, however I unfortunately don't have access to the underlying data model that drives the ItemsSource.  This is a custom control I'm building that builds off a ListBox.  So I know the ItemsSource, but users of the control actually provide the data object behind the scenes which I have no idea about.

    Any other way to accomplish this?
  • Thursday, July 02, 2009 6:16 PMJRQ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Consumer of your control will normally bind to the SelectedXXX properties to update their model.

  • Thursday, July 02, 2009 6:18 PMMariano O. Rodriguez Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    You can make a wrapper over that class and raise the property changed. Other option is refresh the binding when you know you have a change.
    http://weblogs.asp.net/marianor/
  • Thursday, July 02, 2009 9:20 PMKofoed Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Refreshing the binding ... that's a possibility, I'll try that.

    You mentioned making a wrapper for the class ... which class are u referring to?
  • Thursday, July 02, 2009 10:42 PMMariano O. Rodriguez Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    I'm referin to the class that you are using to bound the items. You can use som patterns to do that, for example MVVM.

    http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
    http://weblogs.asp.net/marianor/