Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Lets break this down.</p> <p><strong>Whats happening</strong></p> <p>Your <code>MyClass</code> will have an override on <code>ToString</code> the generates a string that represents the value of the object which will be function of the properties you've assigned. When you assign the object to <code>Content</code> the SL framework it calls the <code>ToString</code> to get some text to display. As you have rightly discovered assigning the same reference doesn't invoke this call again.</p> <p><strong>A simple solution that will get you going and is likely good enough</strong></p> <pre><code>Button button = new Button(); MyClass myClass = new MyClass() { A = 1, B = 2 }; button.Tag = myClass; button.Content = myClass.ToString(); stackPanel.Children.Add(button); </code></pre> <p>...</p> <pre><code>Button button = (Button)stackPanel.Children[0]; MyClass myClass = (MyClass)button.Tag; myClass.A = 421; button.Content = myClass.ToString(); </code></pre> <p>The <code>Tag</code> property is effectively a peg on which we can hang a related object that we may need later. In this case we are performing the ToString() operation hance Content always gets a different reference to what it already has (at least always when the values are actually different).</p> <p><strong>A more traditional SL solution but not necessarily desirable in this case</strong></p> <p>To achieve what you want you would need to do several things. First you will need to expose a property of string type on <code>MyClass</code> that contains the string you want to present, lets call it <code>DisplayValue</code>.</p> <pre><code>public string DisplayValue { get { return String.Format("{0} {1}", A, B); } } </code></pre> <p>Now you need to bind the result of this to the Button in some way so that it can display the value. A start would be to assign the instance of <code>MyClass</code> to the <code>DataContext</code> proeprty instead of <code>Tag</code>. However <code>Button</code> doesn't' have a <code>Text</code> property where in other cases you would bind the <code>DisplayProperty</code>. Hence you need to add a <code>TextBlock</code> as the content of the button and bind its <code>Text</code> property:-</p> <pre><code>Button button = new Button(); TextBlock tb = new TextBlock(); tb.SetBinding(TextBlock.TextProperty, new Binding("DisplayValue")); button.Content = tb; button.DataContext = myClass; </code></pre> <p>However in order for control to know that the bound value has been changed your <code>MyClass</code> needs to implement <code>INotifyPropertyChanged</code>. You add this to your class like this:-</p> <pre><code> public MyClass : INotifyChanged { // ... the rest of you code public event PropertyChangedEventHandler PropertyChanged; } </code></pre> <p>You then need to raise the event whenever a property is changed a typical example:-</p> <pre><code> private int _A public int A { get { return _A; } set { _A = value; if (this.PropertyChanged != null) this.PropertyChanged(this, new PropertyChangedEventArgs("A")); } } </code></pre> <p>Still this typical example doesn't help. In your case you've bound to <code>DisplayValue</code> and since the value of A influences its value your class also needs to notify <code>DisplayValue</code> changed:-</p> <pre><code> if (this.PropertyChanged != null) { this.PropertyChanged(this, new PropertyChangedEventArgs("A")); this.PropertyChanged(this, new PropertyChangedEventArgs("DisplayValue")); } </code></pre> <p>As you can see its significantly more complex than the other solution yet doesn't seem to deliver any more. The key advantage is that logic in you application can now concentrate on mutating object values without having concern itself with keeping the display up-to-date, that takes care of itself. Hence it enables good separation of logical code from presentation code.</p> <p>So why might this not be desirable? It depends really what your real class represents but in effect the <code>DisplayValue</code> is dictating how the objects state should be represented on screen. However does this responsibility really rest with the class or is that not the responsibility of presentation code, typically the latter is true and the <code>DisplayValue</code> is generally not a good idea (and is the reason why few class actually bother overriding <code>ToString</code>).</p>
 

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