Exemplo n.º 1
0
 private void SetUpServerItem(IDotvvmRequestContext context, object item, int index, IValueBinding itemBinding, DataItemContainer container)
 {
     container.DataItemIndex = index;
     container.SetDataContextForItem(itemBinding, index, item);
     container.SetValue(Internal.PathFragmentProperty, GetPathFragmentExpression() + "/[" + index + "]");
     container.ID = index.ToString();
 }
Exemplo n.º 2
0
        private void DataBind(IDotvvmRequestContext context)
        {
            Children.Clear();
            emptyDataContainer = null;
            head = null;

            var dataSourceBinding = GetDataSourceBinding();
            var dataSource        = DataSource;


            if (dataSource is IRefreshableGridViewDataSet refreshableDataSet)
            {
                CallGridViewDataSetRequestRefresh(refreshableDataSet);
            }

            var sortCommand =
                dataSource is ISortableGridViewDataSet sortableSet && sortableSet.SortingOptions is ISortingOptions sortOptions ?
                expr => {
                if (sortOptions.SortExpression == expr)
                {
                    sortOptions.SortDescending ^= true;
                }
                else
                {
                    sortOptions.SortExpression = expr;
                    sortOptions.SortDescending = false;
                }
                (sortableSet as IPageableGridViewDataSet)?.GoToFirstPage();
            } :
            SortChanged;

            // WORKAROUND: DataSource is null => don't throw exception
            if (sortCommand == null && dataSource == null)
            {
                sortCommand = s => {
                    throw new DotvvmControlException(this, "Cannot sort when DataSource is null.");
                };
            }

            CreateHeaderRow(context, sortCommand);
            var index = 0;

            if (dataSource != null)
            {
                var itemBinding = GetItemBinding();
                foreach (var item in GetIEnumerableFromDataSource())
                {
                    // create row
                    var placeholder = new DataItemContainer {
                        DataItemIndex = index
                    };
                    placeholder.SetDataContextForItem(itemBinding, index, item);
                    placeholder.SetValue(Internal.PathFragmentProperty, GetPathFragmentExpression() + "/[" + index + "]");
                    placeholder.ID = index.ToString();
                    Children.Add(placeholder);
                    CreateRowWithCells(context, placeholder);

                    index++;
                }
                numberOfRows = index;
            }
            else
            {
                numberOfRows = 0;
            }

            // add empty item
            if (EmptyDataTemplate != null)
            {
                emptyDataContainer = new EmptyData();
                emptyDataContainer.SetValue(EmptyData.VisibleProperty, GetValueRaw(VisibleProperty));
                emptyDataContainer.SetBinding(DataSourceProperty, dataSourceBinding);
                EmptyDataTemplate.BuildContent(context, emptyDataContainer);
                Children.Add(emptyDataContainer);
            }
        }