Microsoft Developer Network > 포럼 홈 > 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?

  • 2009년 7월 2일 목요일 오후 5: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?

답변

모든 응답

  • 2009년 7월 2일 목요일 오후 5: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/
  • 2009년 7월 2일 목요일 오후 6: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?
  • 2009년 7월 2일 목요일 오후 6:16JRQ 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     

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

  • 2009년 7월 2일 목요일 오후 6: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/
  • 2009년 7월 2일 목요일 오후 9: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?
  • 2009년 7월 2일 목요일 오후 10: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/