Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. DataBinding issue

DataBinding issue

Scheduled Pinned Locked Moved C#
helpwpfwcftutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GDavy
    wrote on last edited by
    #1

    Hello people, I have the following problem. I have a class with a property like in the below example:

    class myClass:INotifyPropertyChanged
    {
    private string theProp;

    public string TheProp
    {
    get{ return theProp; }
    set
    {
    if( string.IsNullOrEmpty(value) )
    theProp = "Another Value";
    else
    theProp = value;
    NotifyPropertyChanged("TheProp");
    }
    }
    /// And a lot of other stuff
    }

    So in case an empty or null is set I set the internal value of the property to a default string. The object is doing this just fine. But the problem I have is with a textBox that is bound to this property. When I delete the text in the textbox, the textbox does not represent the different value. Probably I'm doing something wrong with the binding, it seems like a common way to set a different value, so I suppose there must be a way to make this work. thanks for any help you might provide. Davy

    D 1 Reply Last reply
    0
    • G GDavy

      Hello people, I have the following problem. I have a class with a property like in the below example:

      class myClass:INotifyPropertyChanged
      {
      private string theProp;

      public string TheProp
      {
      get{ return theProp; }
      set
      {
      if( string.IsNullOrEmpty(value) )
      theProp = "Another Value";
      else
      theProp = value;
      NotifyPropertyChanged("TheProp");
      }
      }
      /// And a lot of other stuff
      }

      So in case an empty or null is set I set the internal value of the property to a default string. The object is doing this just fine. But the problem I have is with a textBox that is bound to this property. When I delete the text in the textbox, the textbox does not represent the different value. Probably I'm doing something wrong with the binding, it seems like a common way to set a different value, so I suppose there must be a way to make this work. thanks for any help you might provide. Davy

      D Offline
      D Offline
      darkelv
      wrote on last edited by
      #2

      public partial class Form1 : Form
      {
      myClass o = new myClass();

          public Form1()
          {
              InitializeComponent();
          }
      
          private void Form1\_Load(object sender, EventArgs e)
          {
              this.textBox1.DataBindings.Add("Text", o, "TheProp", false, DataSourceUpdateMode.OnPropertyChanged, string.Empty);
          }
      }
      
      class myClass : INotifyPropertyChanged
      {
          public event PropertyChangedEventHandler PropertyChanged;
      
          private string theProp;
          public string TheProp
          {
              get { return theProp; }
              set
              {
                  if (string.IsNullOrEmpty(value))
                      theProp = "Another Value";
                  else
                      theProp = value;
                  NotifyPropertyChanged("TheProp");
              }
          }
          private void NotifyPropertyChanged(String info)
          {
              if (PropertyChanged != null)
              {
                  PropertyChanged(this, new PropertyChangedEventArgs(info));
              }
          }
      
      }
      
      G 1 Reply Last reply
      0
      • D darkelv

        public partial class Form1 : Form
        {
        myClass o = new myClass();

            public Form1()
            {
                InitializeComponent();
            }
        
            private void Form1\_Load(object sender, EventArgs e)
            {
                this.textBox1.DataBindings.Add("Text", o, "TheProp", false, DataSourceUpdateMode.OnPropertyChanged, string.Empty);
            }
        }
        
        class myClass : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
        
            private string theProp;
            public string TheProp
            {
                get { return theProp; }
                set
                {
                    if (string.IsNullOrEmpty(value))
                        theProp = "Another Value";
                    else
                        theProp = value;
                    NotifyPropertyChanged("TheProp");
                }
            }
            private void NotifyPropertyChanged(String info)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(info));
                }
            }
        
        }
        
        G Offline
        G Offline
        GDavy
        wrote on last edited by
        #3

        Thanks! It works like a charm now... I had set the databinding via the Visual Studio GUI using a BindingSource. Apparantly it doesn't set the parameters exactly the same way. Setting the binding myself the way you showed made it work. Greetings, Davy

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups