开发者

Binding to interface property, represents null while binding

I have to bind my GUI to the interface:

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 someOb开发者_C百科ject;
  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.
}

Now in my application main.xaml.cs file, I have field

private IMain theMain;
Then I set DataContext of my window to IMain field:

theMain = new CMain(... some args ...);
this.DataContext = this.theMain;
...initialize theMain...

in XAML it looks like this:

Label Content="{Binding Path=MyObject.SomeString}" Name="label1"/>
Label Content="{Binding Path=MyMidObj.SomeString}" Name="label2"/>
Label Content="{Binding Path=MyCollectionObject.SomeString}" Name="label3"/>

Initialization like I wrote is mostly in other threads than main app thread. Still, in every "state object" there is SomeString property, which calls NotifyPropertyChanged event. SomeString is updated whenever thread change object state. Window GUI has some labels that represents SomeString of proper objects.

I don't know why only MyObject of IMain interface is updating binded label when SomeString of TopObject changes. Labels for MidObj and MyCollectionObject are empty somehow.

Sorry for my english, I hope my question is not to confusing ;)

Thanks

Joe


what happens if you set your binding to:

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜