Is it possible to access a Silverlight DataGrid column by name and not by column index?

Does anyone know if it is possible to access a DataGrid column using its x: name (as defined in xaml) from the code behind?

I know that I can use the following code:

myDataGridList.Columns[0].Header = "Some Data"; 

.. but I would rather use something like this if possible:

 myDataGridList.Columns["ColumnName"].Header = "Some Data"; 

Thanks in advance.

+6
silverlight xaml
source share
1 answer

You can extend the ObservableCollection with some Linq or foreach loop to do a linear column search.

 public static class MyExtensions { public static DataGridColumn GetByName(this ObservableCollection<DataGridColumn> col, string name) { return col.SingleOrDefault(p => (string)p.GetValue(FrameworkElement.NameProperty) == name ); } } 

Then you can call this instead of the Columns property:

 myGrid.Columns.GetByName("theName"); 
+13
source share

All Articles