开发者

What conditions have to be fulfilled in order to edit custom collection via WPF DataGrid?

In short

I have custom collection which I would like to show and edit (GUI way) using datagrid. The first part -- showing -- works. The second -- editing -- does not.

On first try of ineractive editing (by user), I get error exception:

'EditItem' is not allowed for this view.

Superficially the collection seems ready for editing, so what are the conditions for editing using datagrid?

Details

My custom collection is 2 dimensional array of bool, implementing IEditableObject. I create datagrid columns manually and after that I assign my collection to ItemSource of the datagrid. IsReadonly for both columns and for entire datagrid is set to false.

   void SetupDisplay()
    {
        ConnectionsGrid.BeginInit();
        ConnectionsGrid.Columns.Clear();

        int count = 0;

        foreach (var conn in Connections)
        {
            var col = new DataGridCheckBoxColumn();
            col.IsReadOnly = false;
            col.Header = count == 0 ? "gr" : count.ToString();
            col.Binding = new Binding(String.Format("[{0}]", count));
            ConnectionsGrid.Columns.Add(col);
            ++count;
        }

        ConnectionsGrid.IsReadOnly开发者_StackOverflow社区 = false;

        ConnectionsGrid.EndInit();

        ConnectionsGrid.ItemsSource = Connections;

        UpdateDisplay();

    }
    void UpdateDisplay()
    {
        this.Dispatcher.BeginInvoke(DispatcherPriority.DataBind, doUpdateDisplay);
    }
    void doUpdateDisplay()
    {

        foreach (var row in Enumerable.Range(0, ConnectionsGrid.Items.Count))
        {
            ConnectionsGrid.GetRow(row).Header = row == 0 ? "gr" : row.ToString();
        }

    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜