Example #1
0
        private static List <T> GetVisualChildCollection <T>(object parent)
            where T : Visual
        {
            var visualCollection = new List <T>();

            DataGridBehavior.GetVisualChildCollection(parent as DependencyObject, visualCollection);

            return(visualCollection);
        }
Example #2
0
        /// <summary>
        /// Gets the visual children.
        /// </summary>
        /// <typeparam name="T">The type of the visuals.</typeparam>
        /// <param name="parent">The parent.</param>
        /// <param name="visualCollection">The collection to fill.</param>
        private static void GetVisualChildCollection <T>([NotNull] DependencyObject parent, ICollection <T> visualCollection)
            where T : Visual
        {
            var count = VisualTreeHelper.GetChildrenCount(parent);

            for (var i = 0; i < count; i++)
            {
                var child = VisualTreeHelper.GetChild(parent, i);

                if (child is T)
                {
                    visualCollection.Add(child as T);
                }

                DataGridBehavior.GetVisualChildCollection(child, visualCollection);
            }
        }
Example #3
0
        /// <summary>
        /// Called when the row number changed.
        /// </summary>
        /// <param name="target">
        /// The target.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private static void RowNumberChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
        {
#pragma warning disable SA1119 // Statement must not use unnecessary parenthesis
            if (!(target is DataGrid dataGrid))
#pragma warning restore SA1119 // Statement must not use unnecessary parenthesis
            {
                return;
            }

            if (!(bool)e.NewValue)
            {
                return;
            }

            void LoadedRowHandler(object sender, DataGridRowEventArgs ea)
            {
                if (DataGridBehavior.GetDisplayRowNumber(dataGrid) == false)
                {
                    dataGrid.LoadingRow -= LoadedRowHandler;

                    return;
                }

                ea.Row.Header = ea.Row.GetIndex();
            }

            dataGrid.LoadingRow += LoadedRowHandler;

            void ItemsChangedHandler(object sender, ItemsChangedEventArgs ea)
            {
                if (DataGridBehavior.GetDisplayRowNumber(dataGrid) == false)
                {
                    dataGrid.ItemContainerGenerator.ItemsChanged -= ItemsChangedHandler;

                    return;
                }

                DataGridBehavior.GetVisualChildCollection <DataGridRow>(dataGrid).ForEach(d => d.Header = new TextBlock {
                    Text = d.GetIndex().ToString(), HorizontalAlignment = HorizontalAlignment.Right
                });
            }

            dataGrid.ItemContainerGenerator.ItemsChanged += ItemsChangedHandler;
        }