Anoobi
Anoobi

Reputation: 23

Array value binding in WPF

I have an object with an Array of doubles property like this:

private double[] _axes;

public double[] Axes
{
    get
    {
        return _axes;
    }
    set
    {
        _axes = value;
        if (PropertyChanged != null)
        {
            NotifyPropertyChanged("Axes[]");
        }
    }
}

This Array is always assigned as a whole, because i get an Array of double out of my database object and set it to the Axes property like this:

object.Axes = GetDataRow(i);

(this is just to demonstrate that i in fact assign the whole Array)

In my XAML now i bind it like this:

 <Label  Content="{Binding Path=Axes[0], UpdateSourceTrigger=PropertyChanged}"

I set the DataContext of the window to the Object that has the Axes property and the UI receives one value from it at program start that i assign in the constructor of the object. However, even though the "Axes[]" event is raised constantly (which i checked from another object), no further values arrive in the UI.

So my question is: CAN this even work? Does Axes[] not correspond to Axes[0]? what could be the problem?

Upvotes: 2

Views: 2242

Answers (3)

Praveen Paulose
Praveen Paulose

Reputation: 5771

Your property name is incorrect in the notification

if (PropertyChanged != null)
{
     NotifyPropertyChanged("Axes");
}

Remove the brackets. This should solve your problem in this case. However, it is recommended to use ObservableCollection for binding a list of objects.

Upvotes: 2

Tim
Tim

Reputation: 15227

The binding system won't look "into" the array for updates. You just need to notify it that the property itself changed. You're currently saying that a property called Axes[] changed, but you don't have a property with that name. It's just Axes

public double[] Axes
{
    get
    {
        return _axes;
    }
    set
    {
        _axes = value;
        if (PropertyChanged != null)
        {
            NotifyPropertyChanged("Axes");
        }
    }
}

If you really need to know if an individual item in the array is changing, then you should be using ObservableCollection, which the binding system does know how to look "into".

Upvotes: 0

SLaks
SLaks

Reputation: 887225

No.

You have no property named Axes[], so that event has no effect.

You should not bind to arrays; instead, use ObservableCollection, and make the property read-only.

Upvotes: 0

Related Questions