C# 绑定到接口属性,表示绑定时为null

C# 绑定到接口属性,表示绑定时为null,c#,.net,wpf,binding,null,C#,.net,Wpf,Binding,Null,我必须将GUI绑定到界面: public interface IMain { public CTopObject MyObject { get; } public CInsideObject MidObj { get; } public CInsideObject MyCollectionObject { get; } } Objects definitions: public class CMain { public CTopObject MyObject { get {

我必须将GUI绑定到界面:

public interface IMain
{
  public CTopObject MyObject { get; }
  public CInsideObject MidObj { get; }
  public CInsideObject MyCollectionObject { get; }
}

Objects definitions:

public class CMain
{
  public CTopObject MyObject { get { return this.myObject; }
  public CInsideObject MidObj { get { return this.MyObject.SomeObject; } }
  public CInsideObject MyCollectionObject 
  { get 
    { 
      if(this.MyObject.Thedict.Contains(0))
        return this.MyObject.TheDict[0]; 
      else
        return default(CInsideObject);

    } 
  } //0 is ofcourse an example
}

public class CTopObject
{
  private string someString;
  public string SomeString
  {
    get { return this.someString; }
    set
    {
      this.someString = value;
      if(this.PropertyChanged!=null) this.PropertyChanged(this, "SomeString");
    }  
  } 
  private ObservableDictionary int, CInsideObject> theDict; //how to open bracket on stackoverflow? ;)
  public ObservableDictionary int, CInsideObject> TheDict
  {
    get { return this.theDict; }
  }
  private CInsideObject someObject;
  public CInsideObject SomeObject
  {
    get { return this.someObject; }
  }

  //There is constructor. After that, there is init method,
  //that creates new thread. The thread updates SomeString,
  //representing object state. After thread raise specified event 
  //callback method begin to initialize SomeObject and 
  //after callback from SomeObject it adds some objects to 
  //TheDict, and initialize them.
}

public class CInsideObject : INotifyPropertyChanged
{
  private string someString;
  public string SomeString
  {
    get { return this.someString; }
    set
    {
      this.someString = value;
      if(this.PropertyChanged!=null) this.PropertyChanged(this, "SomeString");
    }  
  }    

   //There is constructor, and init method that creates new thread. 
   //This thread updates SomeString that represents actual state of object.
}
现在在我的应用程序main.xaml.cs文件中,我有一个字段
private IMain theMain;
在XAML中,它如下所示:

Label Content="{Binding Path=MyObject.SomeString}" Name="label1"/>
Label Content="{Binding Path=MyMidObj.SomeString}" Name="label2"/>
Label Content="{Binding Path=MyCollectionObject.SomeString}" Name="label3"/>
像我写的那样,初始化主要是在主应用程序线程之外的其他线程中进行的。不过,在每个“状态对象”中都有一些字符串属性,它调用NotifyPropertyChanged事件。每当线程更改对象状态时,都会更新SomeString。WindowGUI有一些标签,表示适当对象的字符串

我不知道为什么只有IMain接口的MyObject在TopObject的某些字符串更改时更新绑定标签。MidObj和MyCollectionObject的标签不知何故是空的

对不起我的英语,我希望我的问题不要让人困惑;)
谢谢

Joe

如果将绑定设置为:

Label Content="{Binding Path=MyObject.SomeObject.SomeString}" Name="label2"/>
Label Content="{Binding Path=MyObject.TheDict}" Name="label3"/>

谢谢你的回答。它工作得很好,但我想知道是否有任何方法可以在GUI和引擎之间实现清晰的接口。只绑定到接口,而不知道“模型”的结构;
Label Content="{Binding Path=MyObject.SomeObject.SomeString}" Name="label2"/>
Label Content="{Binding Path=MyObject.TheDict}" Name="label3"/>