× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Auto bind data to grid.textblock

Codementor User
Feb 05, 2015
<p>Your dataReceived collection needs to be declared like this because it is the subject of a binding...</p> <pre><code> public ObservableCollection&lt;Data&gt; DataReceivedCollection { get; set; } </code></pre> <p>And in the initialization code, it needs to be instantiated like this...</p> <pre><code> DataReceivedCollection = new ObservableCollection&lt;Data&gt;(); </code></pre> <p>And your data class should be declared something like this (not all properties declared)</p> <pre><code>public class Data : INotifyPropertyChanged { private string _textFirst; public string TextFirst { [DebuggerStepThrough] get { return _textFirst; } [DebuggerStepThrough] set { if (value != _textFirst) { _textFirst = value; OnPropertyChanged("TextFirst"); } } } private string _textSecond; public string TextSecond { [DebuggerStepThrough] get { return _textSecond; } [DebuggerStepThrough] set { if (value != _textSecond) { _textSecond = value; OnPropertyChanged("TextSecond"); } } } #region INotifyPropertyChanged Implementation public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string name) { var handler = System.Threading.Interlocked.CompareExchange(ref PropertyChanged, null, null); if (handler != null) { handler(this, new PropertyChangedEventArgs(name)); } } #endregion } </code></pre> <p>Doing these things will ensure that the binding engine gets the information it needs to populate your List Box. </p> <p>This is only a start that will give you some better results. As mentioned in the commentary, your next port of call is to take up a study of INotifyPropertyChanged.</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/23317036/Auto%20bind%20data%20to%20grid.textblock/23318309">Stack Overflow</a>.</p>
comments powered by Disqus