Monthly Archives: April 2011

Using attached properties to style RowDefinitions of a Grid in WPF

The below sample shows how you can use attached dependency properties to style a Grid’s RowDefinitions. This specific case shows how a single property can be set to specify the number of rows in a Grid.

<Window
    x:Class="GridExtensions.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:GridExtensions="clr-namespace:GridExtensions"
    Title="MainWindow"
    Height="350"
    Width="525">
    <Grid>
        <Grid.Style>
            <Style>
                <Setter Property="GridExtensions:Griddier.RowsCount" Value="4"/>
            </Style>
        </Grid.Style>
        <TextBlock Grid.Row="0" Text="0"/>
        <TextBlock Grid.Row="1" Text="1"/>
        <TextBlock Grid.Row="2" Text="2"/>
        <TextBlock Grid.Row="3" Text="3"/>
    </Grid>
</Window>
namespace GridExtensions
{
    using System;
    using System.Windows;
    using System.Windows.Controls;

    public static class Griddier
    {
        #region RowsCount

        /// <summary>
        /// RowsCount Attached Dependency Property
        /// </summary>
        public static readonly DependencyProperty RowsCountProperty =
            DependencyProperty.RegisterAttached("RowsCount", typeof(int), typeof(Griddier),
                new FrameworkPropertyMetadata((int)0,
                    new PropertyChangedCallback(OnRowsCountChanged)));

        /// <summary>
        /// Gets the RowsCount property. This dependency property 
        /// indicates the number of star-heighted row definitions to include with the grid.
        /// </summary>
        public static int GetRowsCount(DependencyObject d)
        {
            return (int)d.GetValue(RowsCountProperty);
        }

        /// <summary>
        /// Sets the RowsCount property. This dependency property 
        /// indicates the number of star-heighted row definitions to include with the grid.
        /// </summary>
        public static void SetRowsCount(DependencyObject d, int value)
        {
            d.SetValue(RowsCountProperty, value);
        }

        /// <summary>
        /// Handles changes to the RowsCount property.
        /// </summary>
        private static void OnRowsCountChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d == null)
                throw new ArgumentNullException("d");
            var grid = d as Grid;
            if (grid == null)
                throw new ArgumentException("d should be of type Grid.", "d");
            int newRowsCount = (int)d.GetValue(RowsCountProperty);

            if (grid.RowDefinitions.Count > newRowsCount)
                while (grid.RowDefinitions.Count > newRowsCount)
                    grid.RowDefinitions.RemoveAt(newRowsCount);

            var gl = (GridLength)(new GridLengthConverter().ConvertFromString("*"));

            for (int i = 0; i < newRowsCount; i++)
            {
                if (grid.RowDefinitions.Count > i)
                    grid.RowDefinitions[i].Height = gl;
                else
                    grid.RowDefinitions.Add(new RowDefinition {Height = gl});
            }
        }

        #endregion
    }
}
Tagged , , , , , ,