Elad Lachmi
Elad Lachmi

Reputation: 10581

Custom control markup validation

I have a custom control which has a columns collection stored as internal property in markup. The markup looks something like this:

<CustomGrid:CompositeGrid ID="myGrid1" runat="server" ReturnToFirstPageOnSort="true"
     PagerArrowsDisapearWhenNextPrevDisabled="true" AlternatingRowStyling="true">
        <Columns>
            <CustomGrid:DataColumn DataSourceID="TestDate" FormatString="dd.MM.yyyy"
                 Sortable="true" Visible="true" />
            <CustomGrid:DataColumn DataSourceID="TestType" />
            <CustomGrid:DataColumn DataSourceID="Referrer" />
            <CustomGrid:ImageColumn DataSourceID="StatusIcon"
                TextDataSourceID="Status" IncludeText="true" ImageAlt="IconAlt"
                ImageOnThe="Right" />
            <CustomGrid:LinkColumn>
                <Links>
                    <CustomGrid:LinkButtonItem CommandArgument="PDFId" CommandName="GetPDF" />
                    <CustomGrid:OpenDescriptionItem />
                </Links>
            </CustomGrid:LinkColumn>
            <CustomGrid:UserDefinedColumn ImplementingColumnType="MyColumn" HeaderText="User Defined Column" />
            <CustomGrid:DescriptionBlock DataSourceID="TestDate" />
        </Columns>
    </CustomGrid:CompositeGrid>

There are several types of column, and one of the types is a description column. I want to "force" the developer using the control to always put the description column last (if it is present at all). Can I add custom markup validation rules? Can I mark the markup with a red squigly line if the description column is placed anywhere but in that last position? Can I fail web site validation if this rule is violated?

Upvotes: 1

Views: 162

Answers (1)

Alan
Alan

Reputation: 7951

I think you should just create a separate property for DescriptionColumn and make it a different base class if you want to make it special. Or, you could just always sort the columns that way regardless of how the user of the control defines the columns.

Update

Well, I decided to give your idea a go and create a custom exception. I think all you need to do is handle the case in your custom control and throw your own exception. No custom XML validation rules, just a normal .NET exception.

Custom XAML Exception

Here's the relevant portion of the code.

public class DataGrid
{
    ObservableCollection<BaseColumn> columns = new ObservableCollection<BaseColumn>();

    public DataGrid()
    {
        columns.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(subClasses_CollectionChanged);
    }

    void subClasses_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        int count = columns.Count(x => x is DescriptionColumn);

        if (count > 1)
        {
            throw new CustomDataGridException("You can only have one description column");
        }
        if (count == 1)
        {
            if (!(columns[columns.Count - 1] is DescriptionColumn))
                throw new CustomDataGridException("Description column must be last");
        }
    }

    public ObservableCollection<BaseColumn> Columns
    {
        get
        {
            return columns;
        }

        set
        {

            if(columns != null)
                columns.CollectionChanged -= subClasses_CollectionChanged;

            columns = value;

            if (columns != null)
                columns.CollectionChanged += subClasses_CollectionChanged;
        }
    }
}

Just make sure to recompile, close and re-open the designer window or it may not update.

Upvotes: 1

Related Questions