Skip to main content
A newer version of this page is available. .

MetadataLocator Class

Allows to register metadata classes.

Namespace: DevExpress.Mvvm.DataAnnotations

Assembly: DevExpress.Mvvm.v18.1.dll

Declaration

public class MetadataLocator :
    IMetadataLocator

Remarks

The example below illustrates how to register metadata for a generic type.


using DevExpress.Mvvm.DataAnnotations;
using DevExpress.Mvvm.Native;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1 {
    class Program {
        static void Main(string[] args) {
            MetadataLocator.Default = MetadataLocator.Create().AddMetadata(typeof(ReferenceDataItemViewModel<>), typeof(ReferenceDataItemViewModelMetadata<>));

            var vm = new ReferenceDataItemViewModel<MyReferenceDataBaseItem>();
            var attrs = MetadataHelper.GetExternalAndFluentAPIAttrbutes(vm.GetType(), "Code");
        }
    }
    public class ReferenceDataBaseItem<T> where T : ReferenceDataBaseItem<T> {
    }
    public class MyReferenceDataBaseItem : ReferenceDataBaseItem<MyReferenceDataBaseItem> {
    }
    public class ReferenceDataItemBaseViewModel {
        public string Code { get; set; }
        public string Description { get; set; }
    }
    public class ReferenceDataItemViewModel<T> : ReferenceDataItemBaseViewModel where T : ReferenceDataBaseItem<T>, new() {
    }

    public class ReferenceDataItemViewModelMetadata<T> where T : ReferenceDataBaseItem<T>, new() {
        public static void BuildMetadata(MetadataBuilder<ReferenceDataItemViewModel<T>> builder) {
            builder.Property(x => x.Code).Required(() => "Code can't be empty");
            builder.Property(x => x.Description).Required(() => "Description can't be empty");
        }
    }

}

This example demonstrates how to use the following attributes to influence columns generated by the GridControl:-DisplayFormat-NumericMask-DateTimeMask-RegExMask You can also use the Fluent API mechanism instead of attributes. Create a class implementing the IMetadataProvider interface. In the IMetadataProvider.BuildMetadata method, assign required metadata:> ```csharp

void IMetadataProvider<Customer>.BuildMetadata(MetadataBuilder<Customer> builder) { builder.Property(p => p.ID).NumericMask(“N0”); builder.Property(p => p.Name).DisplayFormatString(“name is: {0}”, true); builder.Property(p => p.Phone).RegExMask(@“\d{2}-\d{2}-\d{2}”); builder.Property(p => p.HiredAt).DateTimeMask(“dd/MM/yyyy”); } To register this metadata, use the following code:>csharp MetadataLocator.Default = MetadataLocator.Create().AddMetadata(typeof(Customer), typeof(DataAnnotationsElement1Metadata)); ```With this approach, you don't need to modify the model class code. This may be useful if you don't have access to this class code.

using DevExpress.Xpf.Mvvm.DataAnnotations;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace colGeneration
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            MetadataLocator.Default = MetadataLocator.Create().AddMetadata(typeof(Customer), typeof(DataAnnotationsElement1Metadata));

            ObservableCollection<Customer> customers = new ObservableCollection<Customer>();
            for (int i = 1; i < 10; i++)
            {
                customers.Add(new Customer() { ID = i, Name = "Name" + i, Phone = "00-00-0" + i % 10, HiredAt = DateTime.Now.AddDays(-i) });
            }
            grid.ItemsSource = customers;

            ObservableCollection<Customer2> customers2 = new ObservableCollection<Customer2>();
            for (int i = 1; i < 10; i++)
            {
                customers2.Add(new Customer2() { ID = i, Name = "Name" + i, Phone = "00-00-0" + i % 10, HiredAt = DateTime.Now.AddDays(-i) });
            }
            grid2.ItemsSource = customers2;
        }
    }
    public class DataAnnotationsElement1Metadata : IMetadataProvider<Customer>
    {
        void IMetadataProvider<Customer>.BuildMetadata(MetadataBuilder<Customer> builder)
        {
            builder.Property(p => p.ID).NumericMask("N0");
            builder.Property(p => p.Name).DisplayFormatString("name is: {0}", true);
            builder.Property(p => p.Phone).RegExMask(@"\d{2}-\d{2}-\d{2}");
            builder.Property(p => p.HiredAt).DateTimeMask("dd/MM/yyyy");
        }
    }
    public class Customer
    {
        public int ID
        {
            get;
            set;
        }
        public string Name
        {
            get;
            set;
        }
        public string Phone
        {
            get;
            set;
        }
        public DateTime HiredAt
        {
            get;
            set;
        }
    }
    public class Customer2
    {
        [NumericMask(Mask = "N0")]
        public int ID
        {
            get;
            set;
        }
        [DisplayFormat(DataFormatString = "name is: {0}")]
        public string Name
        {
            get;
            set;
        }
        [RegExMask(Mask = @"\d{2}-\d{2}-\d{2}")]
        public string Phone
        {
            get;
            set;
        }
        [DateTimeMask(Mask = "dd/MM/yyyy")]
        public DateTime HiredAt
        {
            get;
            set;
        }
    }
}

The following code snippets (auto-collected from DevExpress Examples) contain references to the MetadataLocator class.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

Implements

Inheritance

Object
MetadataLocator
See Also