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

为什么此文本框绑定示例在WPF中有效,但在Silverlight中无效?

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

    为什么在下面 银光 当我:

    • 更改中的默认文本 第一 文本框
    • 将光标移动到 第二 文本框(即文本框) 集中注意力 第一个文本框
    • 单击 按钮

    里面 按钮处理程序 财产 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
        }
    }
    
    2 回复  |  直到 15 年前
        1
  •  3
  •   Scrappydog    15 年前

    如果在第一个文本框上显式指定twoway绑定,是否修复它?

    <TextBox 
        Text="{Binding InputText, Mode=TwoWay}"
        Height="200"
        Width="600"
        Margin="0 0 0 10"/>
    
        2
  •  0
  •   Edward Tanguay    15 年前

    好吧,好像我只需要定义 模式= TWOWAY 它的工作原理是:

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

    奇怪的是,它在WPF中工作时没有明确指定模式=twoway。

    推荐文章