Привязка к свойству интерфейса, при связывании представляет ноль - PullRequest
0 голосов
/ 26 апреля 2011

Мне нужно привязать мой графический интерфейс к интерфейсу:

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 приложения у меня есть поле

<code>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 обновляется всякий раз, когда поток изменяет состояние объекта.В графическом интерфейсе окна есть несколько меток, представляющих SomeString соответствующих объектов.

Я не знаю, почему только MyObject интерфейса IMain обновляет привязанную метку при изменении SomeString объекта TopObject.Метки для MidObj и MyCollectionObject как-то пусты.

Извините за мой английский, надеюсь, мой вопрос не запутывает;)
Спасибо
Джо

1 Ответ

0 голосов
/ 26 апреля 2011

что произойдет, если вы установите привязку на:

Label Content="{Binding Path=MyObject.SomeObject.SomeString}" Name="label2"/>
Label Content="{Binding Path=MyObject.TheDict}" Name="label3"/>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...