0

Is it usable or this doesn't work: to change the Text Box.Text and the property behind to change can a binding of this type be made(i know that this can be made with an event from Text Box, i am looking for some kind of binding that can be made) ? Should i just use Text Box.Text in my cod?

<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; }
H.B.
  • 166,899
  • 29
  • 327
  • 400
Sas Gabriel
  • 1,544
  • 2
  • 20
  • 27
  • 1
    any extra explanation needed? – Sas Gabriel Jul 20 '12 at 18:13
  • 1
    Its very unclear what you are asking here. WPF is built in two layers: the UI layer and the Data layer. Bindings are used to pull data from the data layer into the UI layer, such as putting the Name field from the data layer into a TextBox. Most bindings are setup TwoWay by default, meaning if you change either the data layer or the UI layer, the value in the other layer will change too. – Rachel Jul 20 '12 at 18:15
  • if i write something in the GUI the property doesn't change – Sas Gabriel Jul 20 '12 at 18:19
  • Are you using MVVM or is this code behind? – Ashwin Chandran Jul 20 '12 at 18:21
  • @user1103707 In order for properties to participate fully in the binding system, the class should implement [INotifyPropertyChanged](http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx) and the properties themselves should raise a property change notification when they get changed. See MSDN's [How to: Implement the INotifyPropertyChanged Interface](http://msdn.microsoft.com/en-us/library/ms229614.aspx) article for more information – Rachel Jul 20 '12 at 18:23
  • i'm not using any MVVM tool, this is the xaml of TextBox code of Add.xaml and the property from Add.xaml.cs, – Sas Gabriel Jul 20 '12 at 18:24
  • i tryed the msdn article and still doesn't work :| – Sas Gabriel Jul 20 '12 at 18:38
  • Check out [the answer to my question](http://stackoverflow.com/questions/11578580/how-to-get-mouse-wheel-to-change-the-background-image/11578987#11578987), you can use similar code to fix your issue. – Matt Ellen Jul 20 '12 at 19:26

2 Answers2

2

If I understand the question correctly, you're asking how to setup a two way binding to the Text property of a TextBox?

<TextBox Text="{Binding Path=YourProperty, Mode=TwoWay}" />
Nathan
  • 1,080
  • 7
  • 16
  • yes, if i change the TextBox.Text from the GUI also the property to change, btw i tried this and it doesn't work – Sas Gabriel Jul 20 '12 at 18:17
  • What do you mean by "doesn't work?" This is basic WPF functionality. Check out [this answer](http://stackoverflow.com/questions/320028/two-way-binding-in-wpf). – Wonko the Sane Jul 20 '12 at 18:23
1

This Makes both your property changes the TextBox and the TextBox changes the property (from MSDN)
Add in your class contructor 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}" />

Hope it helps

HichemSeeSharp
  • 3,240
  • 2
  • 22
  • 44
  • it doesn't work, btw i have the property in public partial class AddClient : Window, INotifyPropertyChanged { and i see that you have a separate class, is this the problem? – Sas Gabriel Jul 20 '12 at 18:46
  • @user1103707 this **will** work *if* you set the `DataContext` on your view to a `Person` Object – Nathan Jul 20 '12 at 18:52
  • What is the purpose of the Eventhandler? I have 3 depending TextBoxes and calculate one if the other two are set. I just do it in the setter and it works. If I had more complicated things to do I would outsourceit to another method but for a simple "a + b = c" I don't see a benefit, – The incredible Jan Jun 16 '22 at 07:05