Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The Issue was the UserControl was throwing a DataBinding error (visible in the Output window while debugging)</p> <p>Because The UserControl's DataContext was set to "Self" in its own xaml, it was looking for the MainPageSelectedText within its own context (it was not looking for the MainPageSelectedText within the "MainPage" which is where you might think it would look, because when you are physically writing/looking at the code that is what is in "context")</p> <p>I was able to get this "working" by setting the Binding in the code behind. Setting the binding in the code behind is the only way to set the UserControl itself as the "Source" of the binding. But this only works if the Binding is TwoWay. OneWay binding will break this code. A better solution altogether would be to create a Silverlight <em>Control</em>, not a <em>UserControl</em>.</p> <p>See Also:</p> <p><a href="http://social.msdn.microsoft.com/Forums/en-US/silverlightcontrols/thread/052a2b67-20fc-4f6a-84db-07c85ceb3303" rel="nofollow noreferrer">http://social.msdn.microsoft.com/Forums/en-US/silverlightcontrols/thread/052a2b67-20fc-4f6a-84db-07c85ceb3303</a></p> <p><a href="http://msdn.microsoft.com/en-us/library/cc278064%28VS.95%29.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/cc278064%28VS.95%29.aspx</a></p> <p>MyCustomUserControl.xaml</p> <pre><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"&gt; &lt;Grid&gt; &lt;StackPanel&gt; &lt;TextBox x:Name="UserControlTextBox" /&gt; &lt;Border BorderBrush="Black" BorderThickness="1"&gt; &lt;TextBlock x:Name="UserControlTextBlock" 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(); //SEE HERE UserControlTextBox.SetBinding(TextBox.TextProperty, new Binding() { Source = this, Path = new PropertyPath("SelectedText"), Mode = BindingMode.TwoWay }); UserControlTextBlock.SetBinding(TextBlock.TextProperty, new Binding() { Source = this, Path = new PropertyPath("SelectedText") }); //SEE HERE } private static void SelectedText_PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { //empty } } } </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