Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF: How to make DataGrid binding with dynamic columns editable?
    text
    copied!<p>I need to bind some data to a DataGrid with variable number of columns. I made it work using following code:</p> <pre><code>int n = 0; foreach (string title in TitleList) { DataGridTextColumn col = new DataGridTextColumn(); col.Header = title; Binding binding = new Binding(string.Format("DataList[{0}]", n++)); binding.Mode = BindingMode.TwoWay; col.Binding = binding; grid.Columns.Add(col); } </code></pre> <p>where DataList is declared as:</p> <pre><code>public ObservableCollection&lt;double&gt; DataList { get; set; } </code></pre> <p>and TitleList is declared as:</p> <pre><code>public ObservableCollection&lt;string&gt; TitleList { get; set; } </code></pre> <p>The problem is that, even though I specified TwoWay binding, it is really one-way. When I click a cell to try to edit, I got an exception "'EditItem' is not allowed for this view". Did I just miss something in the binding expression? </p> <p>P.S. I found an article from Deborah <a href="http://msmvps.com/blogs/deborahk/archive/2011/01/23/populating-a-datagrid-with-dynamic-columns-in-a-silverlight-application-using-mvvm.aspx" rel="nofollow">"Populating a DataGrid with Dynamic Columns in a Silverlight Application using MVVM"</a>. However, I had hard time to make it work for my case (specifically, I can't make the header binding work). Even if it worked, I'm still facing issues like inconsistent cell styles. That's why I'm wondering if I could make my above code work - with a little tweak? </p> <p>EDIT: I found another post which might be related to my problem: <a href="http://codecopy.wordpress.com/2009/09/10/implicit-two-way-binding/" rel="nofollow">Implicit Two Way binding</a>. It looks if you bind to a list of string to a TextBox using </p> <pre><code>&lt;TextBox Text="{Binding}"/&gt; </code></pre> <p>You will get an error like "Two-way binding requires Path or XPath". But the problem can easily be fixed by using </p> <pre><code>&lt;TextBox Text="{Binding Path=DataContext, RelativeSource={RelativeSource Self}}"/&gt; </code></pre> <p>or </p> <pre><code>&lt;TextBox Text="{Binding .}"/&gt; </code></pre> <p>Can anybody give me a hint if my problem can be solved in a similar way? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload