开发者

Silverlight: Using DataContextProxy to access ObserableCollection elements

I'm trying to get binding working in the header of a grid column which does not have access to the DataContext. To give it access, I used the DataContextProxy described here: http://weblogs.asp.net/dwahlin/archive/2009/08/20/creating-a-silverlight-datacontext-proxy-to-simplify-data-binding-in-nested-controls.aspx

This is a simplified version of my ViewModel:

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

 public String MyString { blah... }
 public ObservableCollection<TabItemViewModel> TabIt开发者_如何学运维ems {blah... }
}

and it works for accessing the MyString using XAML like this:

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

but I'm not sure how to get it to point at the ErrorHeading inside the observable collection of TabItemViewModels...

public class TabItemViewModel : INotifyPropertyChanged
{
 private string _errorHeading;

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

            RaisePropertyChanged("ErrorHeading");
        }
    }



}

I tried it like this:

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

but I dont think you can dig into the ObservableCollection like this - I'm not even sure how it knows which element in the collection to look at.


In your TabItemViewModel implementation, you've defined ErrorHeading two times.

At one place you write

 RaisePropertyChanged("ErrorHeading");

while another place you've written

 OnPropertyChanged("ErrorHeading");

Looks like there is some serious problem with your code. On the top of it, you've not implemented the interface INotifyPropertyChanged in your TabItemViewModel.

So fix these problems first. Maybe, then you would be able to do something more elegant in your code. :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜