Note that there are some explanatory texts on larger screens.

plurals
  1. POSilverlight UserControl Custom Property Binding
    text
    copied!<p>What is the proper way to implement Custom Properties in Silverlight UserControls?</p> <p>Every "Page" in Silverlight is technically a UserControl (they are derived from the UserControl class). When I say UserControl here, I mean a Custom UserControl that will be used inside many different pages in many different scenarios (similar to an ASP.NET UserControl).</p> <p>I would like the Custom UserControl to support Binding and not rely on the Name of the Property it is binding to, to always be the same. Instead, I would like the UserControl itself to have a property that the Controls inside the UserControl bind to, and the ViewModels outside the UserControl also bind to. (please see the example below)</p> <p>Binding within the UserControl works, Binding within the MainPage works, The Binding I set up between the MainPage and the UserControl does not work. Specifically this line:</p> <pre class="lang-xml prettyprint-override"><code>&lt;myUserControls:MyCustomUserControl x:Name="MyCustomControl2" SelectedText="{Binding MainPageSelectedText, Mode=TwoWay}" Width="200" Height="50" /&gt; </code></pre> <p>example output:<br> <img src="https://i690.photobucket.com/albums/vv264/cfalc0n/SilverlightUserControlCustomPropert.png" alt="alt text"></p> <p>MainPage.xaml</p> <pre class="lang-xml prettyprint-override"><code>&lt;UserControl x:Class="SilverlightCustomUserControl.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:myUserControls="clr-namespace:SilverlightCustomUserControl" mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480" DataContext="{Binding RelativeSource={RelativeSource Self}}"&gt; &lt;Canvas x:Name="LayoutRoot"&gt; &lt;StackPanel Orientation="Vertical"&gt; &lt;TextBlock Text="UserControl Binding:" Width="200"&gt;&lt;/TextBlock&gt; &lt;myUserControls:MyCustomUserControl x:Name="MyCustomControl2" SelectedText="{Binding MainPageSelectedText, Mode=TwoWay}" Width="200" Height="50" /&gt; &lt;TextBlock Text="MainPage Binding:" Width="200"&gt;&lt;/TextBlock&gt; &lt;TextBox Text="{Binding MainPageSelectedText, Mode=TwoWay}" Width="200"&gt;&lt;/TextBox&gt; &lt;Border BorderBrush="Black" BorderThickness="1"&gt; &lt;TextBlock Text="{Binding MainPageSelectedText}" Width="200" Height="24"&gt;&lt;/TextBlock&gt; &lt;/Border&gt; &lt;/StackPanel&gt; &lt;/Canvas&gt; &lt;/UserControl&gt; </code></pre> <p>MainPage.xaml.cs</p> <pre><code>namespace SilverlightCustomUserControl { public partial class MainPage : UserControl, INotifyPropertyChanged { //NOTE: would probably be in a ViewModel public string MainPageSelectedText { get { return _MainPageSelectedText; } set { string myValue = value ?? String.Empty; if (_MainPageSelectedText != myValue) { _MainPageSelectedText = value; OnPropertyChanged("MainPageSelectedText"); } } } private string _MainPageSelectedText; public MainPage() { InitializeComponent(); } #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string name) { PropertyChangedEventHandler ph = this.PropertyChanged; if (ph != null) ph(this, new PropertyChangedEventArgs(name)); } #endregion } } </code></pre> <p>MyCustomUserControl.xaml</p> <pre class="lang-xml prettyprint-override"><code>&lt;UserControl x:Class="SilverlightCustomUserControl.MyCustomUserControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" DataContext="{Binding RelativeSource={RelativeSource Self}}"&gt; &lt;Grid&gt; &lt;StackPanel&gt; &lt;TextBox Text="{Binding SelectedText, Mode=TwoWay}" /&gt; &lt;Border BorderBrush="Black" BorderThickness="1"&gt; &lt;TextBlock Text="{Binding SelectedText}" Height="24"&gt;&lt;/TextBlock&gt; &lt;/Border&gt; &lt;/StackPanel&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>MyCustomUserControl.xaml.cs</p> <pre><code>namespace SilverlightCustomUserControl { public partial class MyCustomUserControl : UserControl { public string SelectedText { get { return (string)GetValue(SelectedTextProperty); } set { SetValue(SelectedTextProperty, value); } } public static readonly DependencyProperty SelectedTextProperty = DependencyProperty.Register("SelectedText", typeof(string), typeof(MyCustomUserControl), new PropertyMetadata("", SelectedText_PropertyChangedCallback)); public MyCustomUserControl() { InitializeComponent(); } private static void SelectedText_PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { //empty } } } </code></pre> <p>References (how I got this far): </p> <p>use DependencyPropertys: <a href="http://geekswithblogs.net/thibbard/archive/2008/04/22/wpf-custom-control-dependency-property-gotcha.aspx" rel="nofollow noreferrer">http://geekswithblogs.net/thibbard/archive/2008/04/22/wpf-custom-control-dependency-property-gotcha.aspx</a></p> <p>use DependencyPropertys, add x:Name to your UserControl - add Binding with ElementName, set Custom property again in the PropertyChangedCallback method: <a href="https://stackoverflow.com/questions/845564/setting-custom-properties-in-usercontrol-via-databinding">Setting Custom Properties in UserControl via DataBinding</a></p> <p>don't use custom properties, rely on underlying datacontext names (I do not like this solution): <a href="https://stackoverflow.com/questions/1145435/wpf-trouble-using-dependency-properties-in-a-usercontrol">wpf trouble using dependency properties in a UserControl</a></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