Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't use the Entity Framework, but I think the pattern for the FruitInfo property is missing of an important piece.</p> <p>The problem is that the binding mechanism does not realize about the new ObservableCollection, because it expect some "notification" way to be alerted. That is, you have several ways to solve your problem:</p> <ul> <li>use a DependencyPropety instead of an ordinary property: every time you set the property the bound controls are also notified.</li> </ul> <p>I'd recommend this solution: reliable and versatile.</p> <ul> <li>implement the INotifyPropertyChanged interface in the class exposing the FruitInfo property (e.g. MainWindow), then fire a PropertyChanged event on any actual FruitInfo's value changing.</li> </ul> <p>This way is also valuable, but it looks useless adding a thing already exposed in any DependencyObject-derived class. The INotifyPropertyChanged fits perfectly for the POCO classes (Plain-Old CLR-Objects).</p> <ul> <li>give a name to the combobox, then set the ItemsSource property explicitly.</li> </ul> <p>It works fine, but you'd lose the benefits of the data-context inheritance, especially within templates.</p> <ul> <li>the pattern you used creates the collection in a "lazy" fashion: consider avoiding the lazy-way, and set the FruitInfo value before the combobox is created/bound.</li> </ul> <p>Doable, but typically may be applied in a few cases. Also requires that you know for sure the sequence of the objects creation. Keep as latest way.</p> <p>== UPDATE</p> <p>Try to modify your code as follows:</p> <pre><code>private ObservableCollection&lt;string&gt; _fruits = new ObservableCollection&lt;string&gt;(); public ObservableCollection&lt;string&gt; FruitInfo { get { using (var context = new Fruit()) { this._fruits.Clear(); foreach (var item in context.Fruits.OrderBy(s =&gt; s.FruitName)) { this._fruits.Add(item.FruitName); } return this._fruits; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload