我想要綁定工作在無法訪問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 - 我什至不知道它是如何知道看哪個元素的收藏。
對不起隊友,這只是一個複製粘貼錯誤,這是我第一次使用stackoverflow網站。我已經實現了INotifyPropertyChanged,但爲了簡潔起見,將其省略。 – scoorey 2010-12-15 23:30:32