【问题标题】:How to get the number of rows in a row template selector - wpf - gridcontrol如何获取行模板选择器中的行数 - wpf - gridcontrol
【发布时间】:2019-07-02 07:06:57
【问题描述】:

我需要在其 RowTemplateSelector 中获取 GridControl 的行数,以便根据该数字更改行模板。 我正在尝试使用传递给 TemplateSelector 的 Select() 方法的容器字段。

【问题讨论】:

    标签: wpf devexpress-wpf


    【解决方案1】:

    您不需要容器对象 - 查看 DX-docs 中的 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-object 可以访问相应的 View-object

    DataViewBase view = row.View;
    

    使用视图对象可以访问相应的网格对象

    DataControlBase grid = view.DataControl;
    

    可以访问 DataControl 意味着您可以访问其项目源

    object o = grid.ItemsSource;
    

    从那里开始,它就是转换和计算您的 ItemsSource 的实际类型的问题。 以下 TemplateSelector 根据 item-count 是小于还是大于 10 返回不同的模板:

    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;
        }
    } 
    

    【讨论】:

      猜你喜欢
      • 2011-10-27
      • 2015-11-30
      • 1970-01-01
      • 1970-01-01
      • 2014-07-14
      • 2018-09-27
      • 2010-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多