【问题标题】:Why does KeyDown event not have access to the current value of bound variable?为什么 KeyDown 事件无法访问绑定变量的当前值?
【发布时间】:2010-03-27 16:23:35
【问题描述】:

在下面的例子中:

  • 我启动程序,输入文字,点击按钮,见上面的文字。按 ENTER 再次查看文字。

但是:

  • 我启动程序,输入文本,按ENTER,查看no文本。

KeyDown 事件似乎无法访问绑定变量的当前值,就好像它始终是“one behind”。

我需要进行哪些更改,以便在按 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
    }
}

【问题讨论】:

  • 有趣,我现在正在调查事件顺序,但是 - 你为什么不使用 KeyUp(我通常用来支持体面的修饰符支持的那个)?同样的问题?
  • 我试过KeyUp,效果一样。 KeyUp的优势是什么?你是什​​么意思体面的修饰符支持?

标签: c# wpf silverlight data-binding events


【解决方案1】:

您的文本框-> 属性绑定仅在文本框失去焦点后发生。当您输入文本并按 Enter 键时,您尚未将焦点设置在表单上的其他任何位置。您可以通过键入文本、单击滚动查看器、然后单击文本框并按 Enter 来证明这一点。然后,您将在查看器中看到您的文本发生变化。

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

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

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

看到页面向下大约四分之一:如何让我的数据绑定文本框在我键入时更新源值?

【讨论】:

  • 仅供参考:请注意,现在正在发送每个更改 - 这可能会导致验证(RIA 验证等)以巨大的代价发生。有时,这没关系,你想要什么,但其他时候……(这就是为什么默认就是这样)。
【解决方案2】:

如果将 AddLine 更改为使用 TheLineTextBox.Text 是否有效?

    void AddLine() 
    { 
        TextContent += TheLineTextBox.Text + Environment.NewLine; 
    } 

【讨论】:

  • 是的,像这样直接引用它是可行的,但我想找到一个可以使用数据绑定的解决方案,只是为了保持更松散的耦合,我有 x:name 实际上只是为了关注.
【解决方案3】:

这种方式用于WPF,另一方面,当使用silverlight时,属性UpdateSourceTrigger只有默认值和显式值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 2021-11-30
    • 2012-09-24
    相关资源
    最近更新 更多