0

I'm got a collection of object and inside the object there are going to be some properties and a dynamic list of objects. I'm my example this is illustrated with the Name and Numbers properties.

Now I got it to look right, but I'm having problems changing the data from code behind. If I change something in the mList inside the MainWindow() it's correctly beeing displayed on my DataGrid, but If I do the same inside a Click even't nothing happens.

I have looked at INotifyPropertyChanged, but that did'nt changed nothing.

Please not that this is a sample project I created for this question

MyList.cs

public class MyObject : IEnumerable<object[]>
{
    public string Name { get; set; }
    public int[] Numbers { get; set; }

    public IEnumerator<object[]> GetEnumerator()
    {
        var obj = new object[Numbers.Length + 1];
        obj[0] = Name;
        for (var i = 0; i < Numbers.Length; i++)
            obj[i + 1] = Numbers[i];

        yield return obj;
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

MainWindow.xaml.cs

private List<MyObject> mList;
    public MainWindow()
    {
        InitializeComponent();

        mList = new List<MyObject>
                    {
                        new MyObject {Name = "List 1", Numbers = new[] {1, 2, 3, 4}},
                        new MyObject {Name = "List 2", Numbers = new[] {1, 2, 3, 4}},
                        new MyObject {Name = "List 3", Numbers = new[] {1, 2, 3, 4}}
                    };

        dataGrid.ItemsSource = mList;
        var columnNames = new[] {"List", "Number 1", "Number 2", "Number 3", "Number 4"};
        AddColumns(dataGrid, columnNames);

        mList[0].Name = "Test";   //Works
        mList[0].Numbers[0] = 123;  //Works

    }
    private void AddColumns(DataGrid gv, string[] columnNames)
    {
        for (int i = 0; i < columnNames.Length; i++)
            gv.Columns.Add(new DataGridTextColumn()
                               {
                                   Header = columnNames[i],
                                   Binding = new Binding(String.Format("[{0}]", i))
                               });

    }

    private void ButtonClick(object sender, RoutedEventArgs e)
    {
        mList[0].Name = "Test test";   //Don't work
        mList[0].Numbers[0] = 234;  //Don't work
    }
}

MainWindow.xaml

<StackPanel>
    <Button Click="ButtonClick">Test</Button>
    <DataGrid Height="Auto" Name="dataGrid" AutoGenerateColumns="False"/>
</StackPanel>

EDIT This is a mofidifed MyObject, I'm still not able to change the Name from code

 public class MyObject : IEnumerable<object[]>, INotifyPropertyChanged
{
    private string _name;
    public string Name
    {
        get { return _name; }
        set { _name = value;
            OnPropertyChanged("Name");
        }
    }

    public int[] Numbers { get; set; }

    public IEnumerator<object[]> GetEnumerator()
    {
        var obj = new object[Numbers.Length + 1];
        obj[0] = Name;
        for (var i = 0; i < Numbers.Length; i++)
            obj[i + 1] = Numbers[i];

        yield return obj;
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}
2
  • at first sight you need to call NotifyPropertyChanged for properties Name and Numbers Commented May 23, 2012 at 10:01
  • See Update, Its still not working. Have I places the NotifyPropertyChanged in a wrong place? Commented May 23, 2012 at 10:09

2 Answers 2

3

The problem is that with the following code

Binding = new Binding(String.Format("[{0}]", i))

you bind each column to a Path like [i].

For example in the case of column 0 (binding to property Name) you need

new Binding("Name")

With this code you should see your datagrid update after executing

mList[0].Name = "Test test";  

The same concept is valid for the other columns

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

Comments

2

your //Works just works because its in the ctor :) you need to implement INotifyPropertyChanged in your MyObject class and raise it when the Property changed.

for your Numbers Property you should take int wrapper wich implements INotifyPropertyChanged too.

1 Comment

I changed the code to use INotifyPropertyChanged on the Name property, But Its still not working.

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.