2010-12-15 39 views
0

我想要綁定工作在無法訪問DataContext的網格列標題中。爲了給它的訪問,我用這裏所描述的DataContextProxy:http://weblogs.asp.net/dwahlin/archive/2009/08/20/creating-a-silverlight-datacontext-proxy-to-simplify-data-binding-in-nested-controls.aspxSilverlight:使用DataContextProxy訪問ObserableCollection元素

這是我的視圖模型的簡化版本:

public class ViewModel : INotifyPropertyChanged 
{ 
private String _myString; 
private ObservableCollection<TabItemViewModel> _tabItems; 

public String MyString { blah... } 
public ObservableCollection<TabItemViewModel> TabItems {blah... } 
} 

,它適用於使用XAML這樣的訪問的MyString:

<TextBlock Text="{Binding Source={StaticResource DataContextProxy}, Path=DataSource.MyString}"/> 

但我不知道如何讓它指向可觀察的TabItemViewModel集合內的ErrorHeading ...

public class TabItemViewModel : INotifyPropertyChanged 
{ 
private string _errorHeading; 

public string ErrorHeading 
    { 
     get { return _errorHeading; } 
     set 
     { 
      _errorHeading = value; 

      RaisePropertyChanged("ErrorHeading"); 
     } 
    } 



} 

我想它是這樣的:

<TextBlock Text="{Binding Source={StaticResource DataContextProxy}, Path=DataSource.TabItems.ErrorHeading}"/> 

,但我不認爲你可以挖掘到像這樣的的ObservableCollection - 我什至不知道它是如何知道看哪個元素的收藏。

回答

0

在您的TabItemViewModel實施中,您已經定義了ErrorHeading兩次。

在一個地方,你寫

RaisePropertyChanged("ErrorHeading"); 

而另一個地方,你已經寫了

OnPropertyChanged("ErrorHeading"); 

貌似有一個與你的代碼中的一些嚴重的問題。最重要的是,您尚未在您的TabItemViewModel中實現接口INotifyPropertyChanged

所以先解決這些問題。也許,那麼你可以在你的代碼中做更優雅的事情。 :-)

+0

對不起隊友,這只是一個複製粘貼錯誤,這是我第一次使用stackoverflow網站。我已經實現了INotifyPropertyChanged,但爲了簡潔起見,將其省略。 – scoorey 2010-12-15 23:30:32