Why does KeyDown event not have access to the current value of bound variable?

Posted by Edward Tanguay on Stack Overflow See other posts from Stack Overflow or by Edward Tanguay
Published on 2010-03-27T16:23:35Z Indexed on 2010/03/27 16:53 UTC
Read the original article Hit count: 491

Filed under:
|
|
|
|

In the example below:

  • I start program, type text, click button, see text above. Press ENTER see text again.

BUT:

  • I start program, type text, press ENTER, see no text.

It seems that the KeyDown event doesn't get access to the current value of the bound variable, as if it is always "one behind".

What do I have to change so that when I press ENTER I have access to the value that is in the textbox so I can add it to the chat window?

alt text

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>

Code-Behind:

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

© Stack Overflow or respective owner

Related posts about wpf

Related posts about Silverlight