Do this in C# code instead of template XAML

2019-08-24 10:53发布

问题:

Is there a way to make column of cells in a Silverlight datagrid be readonly in editmode with C# code instead of setting up a entire datagrid as a template in a resource file?

UPDATE
Found an example piece of code - http://forums.silverlight.net/forums/p/17483/58189.aspx

In the response by slhungry midway into the thread. He has example code written as a XAML template. Can you write this in C#?

回答1:

Well, you can certainly create DataGridColumns programmatically in your codebehind and add them to your DataGrid:

DataGridColumn myColumn = new DataGridColumn();

Then you can easily configure the properties:

myColumn.Header = "tyndall's New Column";

myColumn.IsReadOnly = true; // All cells in column will be readonly

You can also programmatically set up binding in the codebehind, if you wish. Finally, you can add this column to the datagrid:

myDataGrid.Columns.Add(myColumn);

Does this help answer your question?