TextChanged事件时更新了XAML文本框

Bas*_*mme 5 .net c# data-binding xaml

我使用XAML和数据绑定(MVVM).当我的用户在TextBox中编写新的文本字符时,我需要更新Label.

XAML

    <Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBox Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="463" Text="{Binding OriginalText}"/>
        <Label Height="28" HorizontalAlignment="Left" Margin="12,41,0,0" Name="label1" VerticalAlignment="Top" Width="463" Content="{Binding ModifiedText}"/>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="400,276,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

视图模型

    class MainViewModel : NotifyPropertyChangedBase
    {
        private string _originalText = string.Empty;
        public string OriginalText
        {
            get { return _originalText; }
            set
            {
                _originalText = value;
                NotifyPropertyChanged("OriginalText");
                NotifyPropertyChanged("ModifiedText");
            }
        }

        public string ModifiedText
        {
            get { return _originalText.ToUpper(); }
        }
    }
Run Code Online (Sandbox Code Playgroud)

我在XAML中添加了一个按钮.按钮什么都不做,但帮助我失去了文本框的焦点.当我失去焦点时,绑定会更新,上面的文字会出现在我的标签中.但是,当文本失去焦点时,数据绑定才会更新.TextChanged事件不会更新绑定.我想强制更新TextChanged事件.我怎样才能做到这一点?我应该使用什么组件?

sll*_*sll 14

 <TextBox Name="textBox1"
      Height="23" Width="463"
      HorizontalAlignment="Left" 
      Margin="12,12,0,0"   
      VerticalAlignment="Top"
      Text="{Binding OriginalText, UpdateSourceTrigger=PropertyChanged}" /> 
Run Code Online (Sandbox Code Playgroud)

MSDN如何:控制TextBox文本更新源时:

TextBox.Text属性的默认UpdateSourceTrigger值为 LostFocus.这意味着如果应用程序具有带有数据绑定TextBox.Text属性的TextBox,则在TextBox失去焦点之前,您键入TextBox的文本不会更新源(例如,当您单击远离TextBox时).

如果希望在键入时更新源,请将绑定的UpdateSourceTrigger设置为PropertyChanged.在以下示例中,TextBox和TextBlock的Text属性绑定到同一源属性.TextBox绑定的UpdateSourceTrigger属性设置为PropertyChanged.