【问题标题】:Silverlight: Using DataContextProxy to access ObserableCollection elementsSilverlight:使用 DataContextProxy 访问 ObserableCollection 元素
【发布时间】:2010-12-15 03:12:28
【问题描述】:

我正在尝试在无法访问 DataContext 的网格列的标题中进行绑定。为了授予它访问权限,我使用了此处描述的 DataContextProxy:http://weblogs.asp.net/dwahlin/archive/2009/08/20/creating-a-silverlight-datacontext-proxy-to-simplify-data-binding-in-nested-controls.aspx

这是我的 ViewModel 的简化版本:

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}"/>

但我不确定如何让它指向 TabItemViewModels 的可观察集合内的 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 - 我什至不确定它如何知道要查看集合中的哪个元素。

【问题讨论】:

    标签: silverlight xaml


    【解决方案1】:

    在您的TabItemViewModel 实现中,您已经定义了两次ErrorHeading。

    你在一个地方写作

     RaisePropertyChanged("ErrorHeading");
    

    你写的另一个地方

     OnPropertyChanged("ErrorHeading");
    

    您的代码似乎存在一些严重问题。最重要的是,您还没有在TabItemViewModel 中实现接口INotifyPropertyChanged。

    所以先解决这些问题。也许,那么您将能够在您的代码中做一些更优雅的事情。 :-)

    【讨论】:

    • 对不起,这只是一个复制粘贴错误,这是我第一次使用 stackoverflow 网站。我已经实现了 INotifyPropertyChanged,但为简洁起见将其省略。
    猜你喜欢
    • 2013-12-10
    • 1970-01-01
    • 1970-01-01
    • 2013-08-15
    • 1970-01-01
    • 1970-01-01
    • 2018-12-09
    • 2011-09-26
    • 2016-03-22
    相关资源
    最近更新 更多