简体   繁体   中英

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

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 (ie take focus off first textbox)
  • click the button

that inside the button handler , the property InputText 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
    }
}

If you explicitly specify TwoWay binding on the first textbox does that fix it?

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

ok, seems as though I just need to define the Mode=TwoWay and it works:

Text="{Binding InputText, Mode=TwoWay}"

Strange that it works in WPF without explicitly specifying Mode=TwoWay.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM