MSDN > 論壇首頁 > Windows Presentation Foundation (WPF) > Can you update the value of an ItemsSource item from the control?
發問發問
 

已答覆Can you update the value of an ItemsSource item from the control?

  • Thursday, 2 July, 2009 17:45Kofoed 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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?

解答

所有回覆

  • Thursday, 2 July, 2009 17:48Mariano O. Rodriguez 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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, 2 July, 2009 18:02Kofoed 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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, 2 July, 2009 18:16JRQ 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

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

  • Thursday, 2 July, 2009 18:18Mariano O. Rodriguez 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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, 2 July, 2009 21:20Kofoed 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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, 2 July, 2009 22:42Mariano O. Rodriguez 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆
    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/