Exemple #1
0
        private void FillClipboardRowContent(DataGridRowClipboardEventArgs args)
        {
            if (args.IsColumnHeadersRow)
            {
                for (int i = args.StartColumnDisplayIndex; i <= args.EndColumnDisplayIndex; i++)
                {
                    DataGridColumn column = ColumnFromDisplayIndex(i);
                    if (!(column.Visibility == Visibility.Visible))
                    {
                        continue;
                    }

                    args.ClipboardRowContent.Add(new DataGridClipboardCellContent(args.Item, column, column.Header));
                }
            }
            else
            {
                int rowIndex = args.GetRowIndexHint();
                if (rowIndex < 0)
                {
                    rowIndex = Items.IndexOf(args.Item);
                }

                // If row has selection
                if (this.SelectedCellsIntersects(rowIndex))
                {
                    for (int i = args.StartColumnDisplayIndex; i <= args.EndColumnDisplayIndex; i++)
                    {
                        DataGridColumn column = ColumnFromDisplayIndex(i);
                        if (!(column.Visibility == Visibility.Visible))
                        {
                            continue;
                        }

                        object cellValue = null;

                        // Get cell value only if the cell is selected - otherwise leave it null
                        if (this.SelectedCellsContains(rowIndex, i))
                        {
                            cellValue = column.OnCopyingCellClipboardContent(args.Item);
                        }

                        args.ClipboardRowContent.Add(new DataGridClipboardCellContent(args.Item, column, cellValue));
                    }
                }
            }
        }
        private static string GetColumnText(DataGridColumn column, object dataContext)
        {
            Contract.Requires(column != null);
            Contract.Requires(dataContext != null);

            try
            {
                if (column.Visibility != Visibility.Visible)
                {
                    return(null);
                }

                return(column.OnCopyingCellClipboardContent(dataContext) as string);
            }
            catch
            {
                return(null);
            }
        }
Exemple #3
0
        /// <summary>
        /// Kopiuje pojedynczą komórkę do schowka.
        /// </summary>
        public void CopyCellContentToClipboard()
        {
            DataGridCellInfo cellInfo;
            DataGridColumn   currentColumn = null;
            object           currentItem   = null;
            string           format        = DataFormats.UnicodeText;

            if (this.CurrentCell != null)
            {
                cellInfo = this.CurrentCell;
            }
            else if (this.SelectedCells.Count > 0)
            {
                cellInfo = this.SelectedCells[0];
            }

            currentColumn = cellInfo.Column;

            if (currentColumn == null)
            {
                if (this.CurrentCell != null && this.CurrentCell.Column != null)
                {
                    currentColumn = this.CurrentCell.Column;
                }
                else if (this.SelectedCells.Count > 0)
                {
                    currentColumn = this.SelectedCells[0].Column;
                }
            }

            if (currentColumn == null)
            {
                return;
            }

            if (cellInfo.Item != null)
            {
                currentItem = cellInfo.Item;
            }
            else if (this.CurrentItem != null)
            {
                currentItem = this.CurrentItem;
            }
            else if (this.SelectedCells.Count > 0 && this.SelectedCells[0].Item != null)
            {
                currentItem = this.SelectedCells[0].Item;
            }

            if (currentItem == null)
            {
                return;
            }

            var value = currentColumn.OnCopyingCellClipboardContent(currentItem);

            if (value == null || value == DBNull.Value)
            {
                value = "";
            }

            StringBuilder sb = new StringBuilder();

            DataGridBoldClipboardHelper.FormatPlainText(value, sb, format);

            DataObject dataObject;
            bool       hasPerms = SecurityHelper.CallerHasAllClipboardPermission() && SecurityHelper.CallerHasSerializationPermission();

            // Copy unconditionally in full trust.
            // Only copy in partial trust if user initiated.
            if (hasPerms)
            {
                (new UIPermission(UIPermissionClipboard.AllClipboard)).Assert();
                try
                {
                    dataObject = new DataObject();
                }
                finally
                {
                    UIPermission.RevertAssert();
                }

                dataObject.SetData(format, sb.ToString(), false /*autoConvert*/);

                // This assert is there for an OLE Callback to register CSV type for the clipboard
                (new SecurityPermission(SecurityPermissionFlag.SerializationFormatter | SecurityPermissionFlag.UnmanagedCode)).Assert();
                try
                {
                    if (dataObject != null)
                    {
                        Clipboard.SetDataObject(dataObject, true /* Copy */);
                    }
                }
                finally
                {
                    SecurityPermission.RevertAll();
                }
            }
        }