代码之家  ›  专栏  ›  技术社区  ›  Edward Tanguay

为什么keydown事件不能访问绑定变量的当前值?

  •  2
  • Edward Tanguay  · 技术社区  · 15 年前

    在下面的示例中:

    • 我启动程序,键入文本,单击 按钮 ,请参见上面的文本。出版社 输入 再次查看文本。

    但是:

    • 我启动程序,输入文本,按 进入 文本。

    似乎按键事件无法访问 现在的 绑定变量的值,就好像它始终是“ 一个落后 “。

    我必须更改什么,以便当我按Enter键时,可以访问文本框中的值,以便将其添加到聊天窗口中?

    alt text http://www.deviantsart.com/upload/1l20kdl.png

    XAML:

    <Window x:Class="TestScroll.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="290" Width="300" Background="#eee">
        <StackPanel Margin="10">
    
            <ScrollViewer Height="200" Width="260" Margin="0 0 0 10"
                          VerticalScrollBarVisibility="Auto"
                          HorizontalScrollBarVisibility="Auto">
                <TextBlock Text="{Binding TextContent}"
                           Background="#fff"/>
            </ScrollViewer>
    
            <StackPanel HorizontalAlignment="Left" Orientation="Horizontal">
                <TextBox x:Name="TheLineTextBox" 
                         Text="{Binding TheLine}" 
                         Width="205" 
                         Margin="0 0 5 0"
                         KeyDown="TheLineTextBox_KeyDown"/>
                <Button Content="Enter" Click="Button_Click"/>
            </StackPanel>
    
        </StackPanel>
    </Window>
    

    代码落后:

    using System;
    using System.Windows;
    using System.Windows.Input;
    using System.ComponentModel;
    
    namespace TestScroll
    {
        public partial class Window1 : Window, INotifyPropertyChanged
        {
    
            #region ViewModelProperty: TextContent
            private string _textContent;
            public string TextContent
            {
                get
                {
                    return _textContent;
                }
    
                set
                {
                    _textContent = value;
                    OnPropertyChanged("TextContent");
                }
            }
            #endregion
    
            #region ViewModelProperty: TheLine
            private string _theLine;
            public string TheLine
            {
                get
                {
                    return _theLine;
                }
    
                set
                {
                    _theLine = value;
                    OnPropertyChanged("TheLine");
                }
            }
            #endregion
    
            public Window1()
            {
                InitializeComponent();
                DataContext = this;
                TheLineTextBox.Focus();
    
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                AddLine();
            }
    
            void AddLine()
            {
                TextContent += TheLine + Environment.NewLine;
            }
    
            private void TheLineTextBox_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.Key == Key.Return)
                {
                    AddLine();
                }
            }
    
            #region INotifiedProperty Block
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected void OnPropertyChanged(string propertyName)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
    
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(propertyName));
                }
            }
            #endregion
        }
    }
    
    3 回复  |  直到 12 年前
        1
  •  3
  •   Anthony Pegram    15 年前

    文本框->属性绑定仅在文本框失去焦点后发生。当您键入文本并按Enter键时,您没有在窗体的任何其他位置设置焦点。您可以通过键入文本,单击滚动查看器,然后单击文本框上的后退并按Enter来演示这一点。然后,您将在查看器中看到您的文本更改。

    要解决这个问题,请将文本框的文本属性更新为该属性。

    Text="{Binding TheLine, UpdateSourceTrigger=PropertyChanged}"
    

    http://blogs.msdn.com/wpfsdk/archive/2006/10/19/wpf-basic-data-binding-faq.aspx

    请参见本页下面大约四分之一的内容: 如何使数据绑定文本框在键入时更新源值?

        2
  •  1
  •   Gabe Timothy Khouri    15 年前

    如果您将addline更改为使用inetextbox.text,它会工作吗?

        void AddLine() 
        { 
            TextContent += TheLineTextBox.Text + Environment.NewLine; 
        } 
    
        3
  •  -1
  •   Dave Clemmer manu    12 年前

    此方法用于WPF,另一方面,当使用Silverlight时, UpdateSourceTrigger 只有默认值和显式值。