1

I need to bind a control for a byte in a byte array. For example,

byte[0]  -> bind with numericupdown1

byte[2] -> bind withe numericupdown3
.
.
.

so that when byte[0] changes , the value in numericupdown1 also changes...

I am able to bind controls to a property, but i could not achieve the above mentioned requirement with byte array

Please help me in this.

Thanks in advance

Regards, Isha

3
  • Is this ASP.NET Webforms? WinForms? WPF? Silverlight? Please tag appropriately. Commented Feb 5, 2012 at 9:16
  • What kind of control are you referring to? Are you using Winforms, WPF, or ASP.NET Webforms? Commented Feb 5, 2012 at 9:18
  • control i am referring is numeric up down in Windows forms applications. Commented Feb 5, 2012 at 9:23

1 Answer 1

2

I don't completely understand what you are binding and in which technology/environment, but you definitely will not be able to do it with an array. This is because arrays do not report when their elements are changing.

If you are in WPF or Silverlight and you are binding your array to some kind of a list in the UI, try to use an ObservableCollection.

If you are in WinForms and you are just manually binding elements to some controls, you will need to create wrappers for each element. It is easy: just create a simple class like:

public sealed class ValueWrapper : INotifyPropertyChanged
{
  public event PropertyChangedEventHandler PropertyChanged;

  public ValueWrapper(byte initialValue) {
     _value = initialValue;
  }      

  private byte _value;
  public byte Value {
     get { return _value; }
     set { 
        _value = value;
        OnPropertyChanged("Value");
     }
  }

  private void OnPropertyChanged(String name)
  {
    if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs(name));
  }
}

Now you want to transform your array of bytes to the array of wrappers. It is trivial, if you use LINQ:

var values = myBytes.Select(x=>new ValueWrapper(x)).ToArray();

Now, bind Value properties of wrapper objects, not bytes (using the Binding class)

And when you change these values:

values[0].Value = 122;

your UI will reflect this change (because of INotifyProperyChanged) and will show that new value.

Good Luck!

Sign up to request clarification or add additional context in comments.

1 Comment

thank you very much for your answer.But when we change the value type (i.e Values[0]) , will the orginal array get affected?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.