Почему в следующем приложении silverlight это когда я:
- изменить текст по умолчанию в first textbox
- переместить курсор в текстовое поле секунда (т.е. снять фокус первое текстовое поле)
- нажмите кнопку
что внутри обработчика кнопки свойство InputText
все еще имеет старое значение "текст по умолчанию"?
Что мне нужно сделать, чтобы привязка работала в Silverlight? Тот же код отлично работает в 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>
Код сзади:
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
}
}