【问题标题】:Why does this textbox binding example work in WPF but not in Silverlight?为什么这个文本框绑定示例在 WPF 中有效,但在 Silverlight 中无效?
【发布时间】:2011-02-08 09:14:18
【问题描述】:

为什么在以下 silverlight 应用程序中,当我:

  • 更改第一个文本框中的默认文本
  • 将光标移动到第二个文本框(即将焦点移开第一个文本框)
  • 点击按钮

按钮处理程序中,属性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
    }
}

【问题讨论】:

    标签: c# silverlight binding


    【解决方案1】:

    好的,好像我只需要定义 Mode=TwoWay 就可以了:

    Text="{Binding InputText, Mode=TwoWay}"
    

    奇怪的是它在 WPF 中没有明确指定 Mode=TwoWay。

    【讨论】:

    【解决方案2】:

    如果您在第一个文本框上明确指定 TwoWay 绑定,是否可以解决问题?

    <TextBox 
        Text="{Binding InputText, Mode=TwoWay}"
        Height="200"
        Width="600"
        Margin="0 0 0 10"/>
    

    【讨论】:

      猜你喜欢
      • 2010-10-15
      • 2014-11-13
      • 1970-01-01
      • 2017-08-31
      • 1970-01-01
      • 2011-03-12
      • 2015-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多