Exemplo n.º 1
0
        private static DataGridCell GetCell(this DataGrid dataGrid, RowColumnIndex index)
        {
            if (index.Column < 0 || index.Column >= dataGrid.Columns.Count)
            {
                return(null);
            }

            if (index.Row < 0 || index.Row >= dataGrid.ItemContainerGenerator.Items.Count)
            {
                return(null);
            }

            var dataGridColumn = dataGrid.Columns[index.Column];
            var row            = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index.Row);

            if (row != null)
            {
                var content = dataGridColumn.GetCellContent(row);
                var cell    = content.Ancestors()
                              .OfType <DataGridCell>()
                              .FirstOrDefault();
                return(cell);
            }

            return(null);
        }
Exemplo n.º 2
0
        /// <inheritdoc />
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string text)
            {
                if (RowColumnIndex.TryParse(text, out var result))
                {
                    return(result);
                }

                var message = $"Could not convert the string '{text}' to an instance of {typeof(RowColumnIndex)})";
                throw new NotSupportedException(message);
            }

            return(base.ConvertFrom(context, culture, value));
        }
Exemplo n.º 3
0
        private static void OnCellItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (Equals(d.GetValue(IsUpdatingProperty), BooleanBoxes.True))
            {
                return;
            }

            d.SetCurrentValue(IsUpdatingProperty, BooleanBoxes.True);
            try
            {
                var dataGrid = (DataGrid)d;
                dataGrid.UnselectAllCells();
                if (e.NewValue == null)
                {
                    dataGrid.SetIndex(null);
                    return;
                }

                for (var r = 0; r < dataGrid.Items.Count; r++)
                {
                    for (var c = 0; c < dataGrid.Columns.Count; c++)
                    {
                        var column   = dataGrid.Columns[c];
                        var cellItem = GetCellItem(column, dataGrid.Items[r]);
                        if (Equals(cellItem, e.NewValue))
                        {
                            var index = new RowColumnIndex(r, c);
                            dataGrid.SetIndex(index);
                            var cell = dataGrid.GetCell(index);
                            if (cell != null)
                            {
                                cell.SetCurrentValue(DataGridCell.IsSelectedProperty, true);
                            }

                            return;
                        }
                    }
                }

                dataGrid.SetIndex(null);
            }
            finally
            {
                d.SetCurrentValue(IsUpdatingProperty, BooleanBoxes.False);
            }
        }