data binding a simple value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sham

    #1

    data binding a simple value

    Hi to all,

    I have an object that has a property numberOfFilesDe leted which is an
    integer. I want to bind a label to it so that when the value changes, the
    label gets updated on the screen.

    I have used the following code to bind the text property of the label to the
    object property.

    private void WinForm_Load(ob ject sender, System.EventArg s e)
    {
    label1.DataBind ings.Add(new Binding("Text", directorySweepO bj,
    "numberOfFilesD eleted"));
    }


    When I do the following :
    private void button1_Click(o bject sender, System.EventArg s e)
    {
    directorySweepO bj.numberOfFile sDeleted =
    directorySweepO bj.numberOfFile sDeleted + 1
    }

    I would expect the label to be updated, but it is not.

    What am I doing wrong?

    Sham.


  • Marc Gravell

    #2
    Re: data binding a simple value

    Your object needs to tell the UI that it has changed; this can be done in
    two ways:

    1.1 style: create a public event on your class called {propertyname}C hanged,
    and call this event when your values change

    2 style: implement the INotifyProperty Changed interface, and call this event
    (specifying the name of the property) when the values change

    Following is an example of the latter:

    public class MyClass : INotifyProperty Changed {
    private int someField;
    public int SomeProperty {
    get {
    return someField;
    }
    set {
    if (someField != value) {
    someField = value;
    OnPropertyChang ed("SomePropert y"); // oh for missing the
    nameof(SomeProp erty) {or nameof(method)? } function...
    }
    }
    }
    protected void OnPropertyChang ed(string propertyName) {
    if (PropertyChange d != null)
    PropertyChanged (this, new
    PropertyChanged EventArgs(prope rtyName));
    }
    public event PropertyChanged EventHandler PropertyChanged ;
    }


    Comment

    • sham

      #3
      Re: data binding a simple value

      Hi Marc,

      Thanks for the reply.

      I just started looking at C# last month in my spare time. So it looks like I
      completely misunderstood databinding. I thought that was the whole point of
      it (i.e. some sort of subject-observer pattern), where by specifying the
      binding would handle the updating, registering of observer, notification
      etc.

      The code I have seen in the past with databinding was always with dataSets
      and not with object properties.

      So, I still need the data binding code -> label1.DataBind ings.Add(new
      Binding("Text", directorySweepO bj,
      "numberOfFilesD eleted"));

      and I then introduce the notification code in my object? Is that correct?

      Thanks once again.
      Sham.

      "Marc Gravell" <mgravell@rm.co m> wrote in message
      news:uf0XvAmJGH A.3224@TK2MSFTN GP09.phx.gbl...[color=blue]
      > Your object needs to tell the UI that it has changed; this can be done in
      > two ways:
      >
      > 1.1 style: create a public event on your class called
      > {propertyname}C hanged, and call this event when your values change
      >
      > 2 style: implement the INotifyProperty Changed interface, and call this
      > event (specifying the name of the property) when the values change
      >
      > Following is an example of the latter:
      >
      > public class MyClass : INotifyProperty Changed {
      > private int someField;
      > public int SomeProperty {
      > get {
      > return someField;
      > }
      > set {
      > if (someField != value) {
      > someField = value;
      > OnPropertyChang ed("SomePropert y"); // oh for missing
      > the nameof(SomeProp erty) {or nameof(method)? } function...
      > }
      > }
      > }
      > protected void OnPropertyChang ed(string propertyName) {
      > if (PropertyChange d != null)
      > PropertyChanged (this, new
      > PropertyChanged EventArgs(prope rtyName));
      > }
      > public event PropertyChanged EventHandler PropertyChanged ;
      > }
      >[/color]


      Comment

      • Marc Gravell

        #4
        Re: data binding a simple value

        Correct;

        IIRC DataSets already include this functionality, which is why you haven't
        seen it so far. If you think about it. it makes sense: you don't want the UI
        constantly probing your objects to see which properties have changed.

        For ref, if you bing to a BindingSource instead (which sits between the UI
        and the objects) you can call ResetBindings() on the source to force a
        re-read from the object (e.g. when you think it has changed), but IMO it is
        better to use notification - this avoids a lot of problems.

        Marc


        Comment

        Working...