Vývoj moderních aplikací na platformě Microsoft .NET

Je vyžadována podpora jazyka JavaScript

Některé stránky na tomto webu vyžadují podporu jazyka JavaScript. Váš webový prohlížeč jazyk JavaScript nepodporuje nebo jazyk JavaScript není povolen.

Chcete-li zjistit, zda webový prohlížeč podporuje jazyk JavaScript nebo jazyk JavaScript chcete povolit, přečtěte si nápovědu k vašemu webovému prohlížeči.


FixedDataGrid.cs

Download file

Toto je zdrojový kód souboru FixedDataGrid.cs

Fixing Silverlight DataGrid Row Auto-Resize only grows row height but won't shrink issue in DataGrid control.

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

namespace SilverlightApplication
{
    /// <summary>
    /// Fixed DataGrid control
    /// </summary>
    /// <remarks>
    /// Fixing issue: DataGrid Row Auto-Resize only grows row height but won't shrink
    /// </remarks>
    public class FixedDataGrid : DataGrid
    {
        /// <summary>
        /// Overrides OnLoadingRow
        /// </summary>
        protected override void OnLoadingRow(DataGridRowEventArgs e)
        {
            if (double.IsNaN(this.RowHeight))
            {
                e.Row.Loaded += Row_Loaded;
            }

            base.OnLoadingRow(e);
        }

        private void Row_Loaded(object sender, RoutedEventArgs e)
        {
            var row = (DataGridRow)sender;
            row.Loaded -= Row_Loaded;

            for (int col = 0; col < this.Columns.Count; col++)
            {
                var cellElement = this.Columns[col].GetCellContent(row);
                if (cellElement != null)
                {
                    cellElement.SizeChanged += CellElement_SizeChanged;
                }
            }            
        }

        private static void CellElement_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            //Fix issue: DataGrid Row Auto-Resize only grows row height but won't shrink
            var dataGrid = GetParentOf<DataGrid>((FrameworkElement)sender);
            if (dataGrid != null && double.IsNaN(dataGrid.RowHeight))
            {
                var row = DataGridRow.GetRowContainingElement((FrameworkElement)sender);

                //Fore recalculating row height
                try
                {
                    dataGrid.RowHeight = 0;
                    row.InvalidateMeasure();
                    row.Measure(row.RenderSize);
                }
                finally
                {
                    //Restore RowHeight
                    dataGrid.RowHeight = double.NaN;
                }
            }
        }

        private static T GetParentOf<T>(FrameworkElement element) where T : FrameworkElement
        {
            while (element != null)
            {
                T item = element as T;
                if (item != null)
                {
                    return item;
                }

                element = (FrameworkElement)VisualTreeHelper.GetParent(element);
            }

            return null;
        }
    }
}