我需要在其RowTemplateSelector中获取GridControl的行数,以便基于该数字更改行模板。 我正在尝试使用传递给TemplateSelector的Select()方法的容器字段。
答案 0 :(得分:2)
您不需要conatiner对象-从DX文档中检出this示例:
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);
}
}
使用RowData对象,您可以访问相应的View对象
DataViewBase view = row.View;
使用视图对象,您可以访问相应的网格对象
DataControlBase grid = view.DataControl;
有权访问DataControl意味着您有权访问其项源
object o = grid.ItemsSource;
从那里开始,转换并计算您的ItemsSource的实际类型即可。 以下TemplateSelector根据项目计数是小于还是大于十而返回不同的模板:
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;
}
}