【问题标题】:Binding to interface property, represents null while binding绑定到接口属性,绑定时表示null
【发布时间】:2011-04-26 09:42:11
【问题描述】:

我必须将我的 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;
然后我将窗口的 DataContext 设置为 IMain 字段:
theMain = new CMain(... some args ...);
this.DataContext = this.theMain;
...initialize 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"/>

像我写的初始化主要是在主应用线程之外的其他线程中。尽管如此,在每个“状态对象”中都有 SomeString 属性,它调用 NotifyPropertyChanged 事件。每当线程更改对象状态时,都会更新 SomeString。 Window GUI 有一些标签,代表 SomeString 适当的对象。

我不知道为什么当 TopObject 的 SomeString 发生变化时,只有 IMain 接口的 MyObject 正在更新绑定标签。 MidObj 和 MyCollectionObject 的标签不知何故为空。

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

【问题讨论】:

    标签: c# .net wpf binding null


    【解决方案1】:

    如果您将绑定设置为:

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

    【讨论】:

    • 谢谢你的答案。它工作正常,但我想知道是否有任何方法可以在 GUI 和引擎之间进行清晰的接口。在不知道“模型”结构的情况下仅绑定到接口;
    猜你喜欢
    • 2010-11-06
    • 1970-01-01
    • 2013-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多