Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Bindings look like the following:</p> <p>XAML</p> <pre><code>&lt;UserControl x:Class="myControl.UserControl1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt; &lt;Grid&gt; &lt;TextBlock Name="_innerCaption" Text="{Binding MyCaption}"/&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>Now you have to set the DataContext of the XAML file to the class you would like to bind to:</p> <pre><code>namespace myControl { /// &lt;summary&gt; /// Interaction logic for UserControl1.xaml /// &lt;/summary&gt; public partial class UserControl1 : UserControl { MyClassToBindTo bindingClass; public UserControl1() { InitializeComponent(); bindingClass = new MyClassToBindTo(); DataContext = bindingClass; } } } </code></pre> <p>And The Binding Class may look like this:</p> <pre><code>public class MyClassToBindTo : NotifyPropertyChangedBase { private string myCaptionString = "hello"; public string MyCaption { get { return myCaptionString } set { myCaptionString = value; OnPropertyChanged("MyCaptionString"); } } // Also implement the INotifyPropertyChanged interface here } </code></pre> <p>Hope this helps you!</p> <p><strong>EDIT:</strong></p> <p>I usually implement the INotifyPropertyChanged stuff in a base class "NotifyPropertyChangedBase:</p> <pre><code>public abstract class PropertyChangedBase : INotifyPropertyChanged { #region Member Variables private static HashSet&lt;string&gt; alreadyCoveredProperties = new HashSet&lt;string&gt;(); #endregion #region INotifyPropertyChanged Members /// &lt;summary&gt; /// Occurs when a property value changes. /// &lt;/summary&gt; public event PropertyChangedEventHandler PropertyChanged; /// &lt;summary&gt; /// Calls registered handlers when a property is changed. /// &lt;/summary&gt; /// &lt;param name="propertyName"&gt;Name of the property.&lt;/param&gt; protected void OnPropertyChanged(string propertyName) { PropertyChangedEventHandler handler = this.PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } } /// &lt;summary&gt; /// Removes all property changed subscription from the event handler. /// &lt;/summary&gt; protected void RemovePropertyChangedSubscription() { if (this.PropertyChanged != null) { this.PropertyChanged -= new PropertyChangedEventHandler(PropertyChanged); this.PropertyChanged = null; } } /// &lt;summary&gt; /// Determines whether the specified property name refers to a property defined for this object. /// &lt;/summary&gt; /// &lt;param name="propertyName"&gt;Name of the property.&lt;/param&gt; /// &lt;returns&gt; /// Returns &lt;c&gt;true&lt;/c&gt; if the provided name refers to a defined property; otherwise, &lt;c&gt;false&lt;/c&gt;. /// &lt;/returns&gt; /// &lt;remarks&gt;Uses reflection.&lt;/remarks&gt; protected bool IsDefinedPropertyName(string propertyName) { string propertyFullName = GetType().FullName + "." + propertyName; if (alreadyCoveredProperties.Contains(propertyFullName)) { return true; } else { PropertyDescriptorCollection col = TypeDescriptor.GetProperties(this); PropertyDescriptor propertyDescriptor = col[propertyName]; // A property exists, if the propertyDescriptor is != null. if (propertyDescriptor != null) { alreadyCoveredProperties.Add(propertyFullName); return true; } else { return false; } } } #endregion } </code></pre>
 

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