2

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?

3 Answers 3

2

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.

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

5 Comments

That won't help if he mutates the array
@Anoobi If it answers your query, do not forget to mark it as the answer.
@SLaks What does it mean to mutate the array?
@PraveenPaulose: Axes[2] = 4;
@SLaks agreed. His question explicitly stated he replaces the entire array. Thats why the Array solution
0

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".

Comments

0

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.

Comments

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.