Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Not sure if it's still relevant but I found a way to style the individual cells with a celltemplate selector. It's a bit hacky because you have to munch around with the Content of the ContentPresenter to get the proper DataContext for the cell (so you can bind to the actual cell item in the cell template):</p> <pre><code> public class DataMatrixCellTemplateSelectorWrapper : DataTemplateSelector { private readonly DataTemplateSelector _ActualSelector; private readonly string _ColumnName; private Dictionary&lt;string, object&gt; _OriginalRow; public DataMatrixCellTemplateSelectorWrapper(DataTemplateSelector actualSelector, string columnName) { _ActualSelector = actualSelector; _ColumnName = columnName; } public override DataTemplate SelectTemplate(object item, DependencyObject container) { // The item is basically the Content of the ContentPresenter. // In the DataMatrix binding case that is the dictionary containing the cell objects. // In order to be able to select a template based on the actual cell object and also // be able to bind to that object within the template we need to set the DataContext // of the template to the actual cell object. However after the template is selected // the ContentPresenter will set the DataContext of the template to the presenters // content. // So in order to achieve what we want, we remember the original DataContext and then // change the ContentPresenter content to the actual cell object. // Therefor we need to remember the orginal DataContext otherwise in subsequent calls // we would get the first cell object. // remember old data context if (item is Dictionary&lt;string, object&gt;) { _OriginalRow = item as Dictionary&lt;string, object&gt;; } if (_OriginalRow == null) return null; // get the actual cell object var obj = _OriginalRow[_ColumnName]; // select the template based on the cell object var template = _ActualSelector.SelectTemplate(obj, container); // find the presenter and change the content to the cell object so that it will become // the data context of the template var presenter = WpfUtils.GetFirstParentForChild&lt;ContentPresenter&gt;(container); if (presenter != null) { presenter.Content = obj; } return template; } } </code></pre> <p>Note: I changed the DataMatrix frome the CodeProject article so that rows are Dictionaries (ColumnName -> Cell Object).</p> <p>I can't guarantee that this solution will not break something or will not break in future .Net release. It relies on the fact that the ContentPresenter sets the DataContext after it selected the template to it's own Content. (Reflector helps a lot in these cases :))</p> <p>When creating the GridColumns, I do something like that:</p> <pre><code> var column = new GridViewColumn { Header = col.Name, HeaderTemplate = gridView.ColumnHeaderTemplate }; if (listView.CellTemplateSelector != null) { column.CellTemplateSelector = new DataMatrixCellTemplateSelectorWrapper(listView.CellTemplateSelector, col.Name); } else { column.DisplayMemberBinding = new Binding(string.Format("[{0}]", col.Name)); } gridView.Columns.Add(column); </code></pre> <p>Note: I have extended ListView so that it has a CellTemplateSelector property you can bind to in xaml</p> <p>@Edit 15/03/2011: I wrote a little article which has a little demo project attached: <a href="http://codesilence.wordpress.com/2011/03/15/listview-with-dynamic-columns/" rel="nofollow noreferrer">http://codesilence.wordpress.com/2011/03/15/listview-with-dynamic-columns/</a></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