I need to get the row-count of a GridControl within its RowTemplateSelector in order to change the rows template based on that number. I am trying to use the container field passed to the Select() method of the TemplateSelector.
You don't need the conatiner-object - check out this sample from the DX-docs:
public class RowTemplateSelector : DataTemplateSelector
{
public DataTemplate EvenRowTemplate { get; set; }
public DataTemplate OddRowTemplate { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container) {
RowData row = item as RowData; //<= mind this line of code!!!!
if (row != null)
return row.EvenRow ? EvenRowTemplate : OddRowTemplate;
return base.SelectTemplate(item, container);
}
}
Using the RowData-object you can access the corresponding View-object
DataViewBase view = row.View;
Using the View-object you can access the corresponding Grid-object
DataControlBase grid = view.DataControl;
Having access to the DataControl means you have access to its item-source
object o = grid.ItemsSource;
From there its a matter of casting and counting your actual type of ItemsSource. The following TemplateSelector returns different Templates depending on wether the item-count is smaller or bigger then ten:
public class RowTemplateSelector : DataTemplateSelector
{
public DataTemplate SmallerThenTenTemplate { get; set; }
public DataTemplate BiggerThenTenTemplate { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
RowData row = item as RowData; //<= mind this line of code!!!!
object itemSource = row.View.DataControl.ItemsSource;
IEnumerable<YourModelType> sourceList = (IEnumerable<YourModelType>)itemSource;
if (sourceList.Count() > 10)
return BiggerThenTenTemplate;
else
return SmallerThenTenTemplate;
}
}