English 中文(简体)
在 DataGrid 中的内数组阵列不更新
原标题:Array inside array in DataGrid not updating

我收集了一些对象, 在对象内部将会有一些属性和动态对象列表。 我举个例子, 用名称和数字属性来说明这一点 。

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.

我看过《通知改变财产》,但是没有改变任何东西。

请不要将此作为我为这个问题创建的样板项目

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();
    }
}

主窗口.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
    }
}

主窗口. 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));
        }
    }
}
最佳回答

问题是使用以下代码

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

将每一柱子都绑在一条像 [i] 一样的道路上。

例如,在栏0(对财产名称具有约束力)的情况下,您需要

new Binding("Name")

用此代码执行后您应该看到您的数据格网更新 。

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

同一概念也适用于其他栏目

问题回答

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.

对于您的数字属性, 您应该使用 Intto carpper wich 执行器 I 通知 Property changed tooded.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签