Why does this textbox binding example work in WPF but not in Silverlight?

Posted by Edward Tanguay on Stack Overflow See other posts from Stack Overflow or by Edward Tanguay
Published on 2010-04-15T13:10:53Z Indexed on 2010/04/15 13:13 UTC
Read the original article Hit count: 225

Filed under:
|
|

Why is it in the following silverlight application that when I:

  • change the default text in the first textbox
  • move the cursor to the second text box (i.e. take focus off first textbox)
  • click the button

that inside the button handler, it still has the old value "default text"?

What do I have to do to get the binding to work in Silverlight? The same code works fine in WPF.

XAML:

<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">
  <StackPanel Margin="10" HorizontalAlignment="Left">

          <TextBox 
        Text="{Binding InputText}"
        Height="200"
        Width="600"
        Margin="0 0 0 10"/>

        <StackPanel HorizontalAlignment="Left">
            <Button Content="Convert" Click="Button_Convert_Click" Margin="0 0 0 10"/>
        </StackPanel>

        <TextBox 
        Height="200"
        Width="600"
        Margin="0 0 0 10"/>

        <TextBlock 
            Text="{Binding OutputText}"/>

        </StackPanel>

</UserControl>

Code Behind:

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
    }
}

© Stack Overflow or respective owner

Related posts about Silverlight

Related posts about binding