Unbounded column

Aug 13, 2009 at 10:13 AM

I have created a simple DataGridUnboundedColumn class that is derived from DataGridBoundColumn and can be used to supply custom text of FrameworkElement for the cell.

You simply subscribe to the CellFormating event and set the CellElement in the EventArgs to the custom element that will be show. It also possible to set just CellText in the EventArgs - in this case the TextBlock with the CellText will be shown in the Grid.

The following examples shows how to use it:

XAML:

    <dg:DataGrid Name="DataGrid1" AutoGenerateColumns="False">
        <dg:DataGrid.Columns>
            <dg:DataGridTextColumn Header="Name" Binding="{Binding Path=Name}"/>
            <myColumn:DataGridUnboundedColumn x:Name="AddressColumn" Header="Address" />
        </dg:DataGrid.Columns>
    </dg:DataGrid>


CODE:

       public MyPage()
        {
            InitializeComponent();
    
            AddressColumn.CellFormating += new UnboundedColumnEventHandler(AddressColumn_CellFormating);
        }
    
        void AddressColumn_CellFormating(object sender, UnboundedColumnEventArgs e)
        {
            IPerson person;
    
            person= e.DataItem as IPerson;
    
            if (person!= null)
                e.CellText = string.Format("{0}, {1} {2}", person.Address, person.PostalCode, person.City);
        }


The DataGridUnboundedColumn  implementation is here:

    class DataGridUnboundedColumn : DataGridBoundColumn
    {
        public event UnboundedColumnEventHandler CellFormating;
    
        public DataGridUnboundedColumn()
        {
            this.IsReadOnly = true;
        }
    
        protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
        {
            return null;
        }
    
        protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
        {
            FrameworkElement shownElement;
            UnboundedColumnEventArgs eventArgs;
    
            if (CellFormating == null)
                return null;
    
    
            eventArgs = new UnboundedColumnEventArgs(cell, dataItem);
    
            // call the event
            CellFormating(this, eventArgs);
    
            shownElement = null;
    
            // check the data set in the eventArgs
            if (eventArgs.CellElement != null)
            {
                // show the set eventArgs.CellElement
                shownElement = eventArgs.CellElement;
            }
            else if (eventArgs.CellText != null)
            {
                // show the CellText in TextBlock
                TextBlock textBlock = new TextBlock();
                textBlock.Text = eventArgs.CellText;
    
                shownElement = textBlock;
            }
            else
            {
                // nothing set
            }
    
            return shownElement;
        }
    }
    
    public delegate void UnboundedColumnEventHandler(object sender, UnboundedColumnEventArgs e);
    
    public class UnboundedColumnEventArgs : EventArgs
    {
        public DataGridCell Cell { get; set; }
        public object DataItem { get; set; }
    
        /// <summary>
        /// The subscriber of the event can set the CellText.
        /// In this case the TextBlock is used to display the text.
        /// NOTE that if CellElement is not null, the CellText will not be used but insted a CellElement will be shown
        /// </summary>
        public string CellText { get; set; }
    
        /// <summary>
        /// The subscribed can set the FrameworkElement that will be shown for this cell.
        /// If the CellElement is null, the CellText will be used to show the TextBlock
        /// </summary>
        public FrameworkElement CellElement { get; set; }
    
        public UnboundedColumnEventArgs()
            : base()
        { }
    
        public UnboundedColumnEventArgs(DataGridCell cell, object dataItem)
            : base()
        {
            Cell = cell;
            DataItem = dataItem;
        }
    }