Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does this textbox binding example work in WPF but not in Silverlight?
    primarykey
    data
    text
    <p>Why is it in the following <strong>silverlight</strong> application that when I:</p> <ul> <li>change the default text in the <strong>first</strong> textbox</li> <li>move the cursor to the <strong>second</strong> text box (i.e. <strong>take focus off</strong> first textbox)</li> <li>click the <strong>button</strong></li> </ul> <p>that inside the <strong>button handler</strong>, the property <code>InputText</code> still has the old value "default text"?</p> <p><strong>What do I have to do to get the binding to work in Silverlight? The same code works fine in WPF.</strong></p> <p><strong>XAML:</strong></p> <pre><code>&lt;UserControl x:Class="TestUpdate123.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" mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"&gt; &lt;StackPanel Margin="10" HorizontalAlignment="Left"&gt; &lt;TextBox Text="{Binding InputText}" Height="200" Width="600" Margin="0 0 0 10"/&gt; &lt;StackPanel HorizontalAlignment="Left"&gt; &lt;Button Content="Convert" Click="Button_Convert_Click" Margin="0 0 0 10"/&gt; &lt;/StackPanel&gt; &lt;TextBox Height="200" Width="600" Margin="0 0 0 10"/&gt; &lt;TextBlock Text="{Binding OutputText}"/&gt; &lt;/StackPanel&gt; &lt;/UserControl&gt; </code></pre> <p><strong>Code Behind:</strong></p> <pre><code>using System.Windows; using System.Windows.Controls; using System.ComponentModel; namespace TestUpdate123 { public partial class MainPage : UserControl, INotifyPropertyChanged { #region ViewModelProperty: InputText private string _inputText; public string InputText { get { return _inputText; } set { _inputText = value; OnPropertyChanged("InputText"); } } #endregion #region ViewModelProperty: OutputText private string _outputText; public string OutputText { get { return _outputText; } set { _outputText = value; OnPropertyChanged("OutputText"); } } #endregion public MainPage() { InitializeComponent(); DataContext = this; InputText = "default text"; } private void Button_Convert_Click(object sender, RoutedEventArgs e) { OutputText = InputText; } #region INotifiedProperty Block public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } } #endregion } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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