绑定以更改属性

时间:2012-07-20 18:07:18

标签: c# wpf xaml binding

它是否可用或这不起作用:更改Text Box.Text和后面的属性以进行更改可以进行此类型的绑定(我知道这可以通过Text Box中的事件进行,我我在寻找可以制作的某种装订方式)? 我应该在我的鳕鱼中使用Text Box.Text吗?

<TextBox Text="{Binding Path=NumeClient, Mode=TwoWay}" Height="23" HorizontalAlignment="Left" Margin="117,21,0,0" Name="textBox1" VerticalAlignment="Top" Width="249" />

public string NumeClient { get; set; }

2 个答案:

答案 0 :(得分:2)

如果我正确理解了这个问题,你问的是如何设置双向绑定到TextBox的Text属性?

<TextBox Text="{Binding Path=YourProperty, Mode=TwoWay}" />

答案 1 :(得分:1)

这使您的属性更改TextBox和TextBox更改属性(来自MSDN)    
添加您的类构造函数DataContext = this;

 public class Person : INotifyPropertyChanged
      {
          private string name;
          // Declare the event
          public event PropertyChangedEventHandler PropertyChanged;
          public string PersonName
          {
              get { return name; }
              set
              {
                  name = value;
                  // Call OnPropertyChanged whenever the property is updated
                  OnPropertyChanged("PersonName");
              }
          }

          // Create the OnPropertyChanged method to raise the event
          protected void OnPropertyChanged(string name)
          {
              PropertyChangedEventHandler handler = PropertyChanged;
              if (handler != null)
              {
                  handler(this, new PropertyChangedEventArgs(name));
              }
          }
      }

XAML:

<TextBox Text="{Binding Path=PersonName, Mode=TwoWay}" />

希望有所帮助