开发者

ObservableCollection and IDataErrorInfo with a XML file

MainWindow() and GetXmlData() work fine and display the xmlfile on gridview. But when I include the XmlNode :IDataErrorInfo class, it stops working.

I like to get all of them to work to display and validate the gridview. Anyone could spot anything I miss? Or explanation, I'm hitting a stop here. It seems like an error between observablecollection.

    public MainWindow()
    {
        InitializeComponent();
        RadGridView testGrid = new RadGridView();
        testGrid.ItemsSource = GetXmlData();
        testGrid.AutoGenerateColumns = false;
        GridViewDataColumn col1 = new GridViewDataColumn();
        col1.DataMemberBinding = new Binding("JobKey") { 
        ValidatesOnDataErrors = true, NotifyOnValidationError = true };                      
        testGrid.Columns.Add(col1);
        LayoutRoot.Children.Add(testGrid);                 
   }

    private static object GetXmlData()
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(@"c:\\JobSetupFile.xml");

        XmlDataProvider provider = new XmlDataProvider();
        provider.IsAsynchronous = false;
        provider.Document = doc;
        provider.XPath = "JobSetup/JobParameters";

        return new ObservableCollection<XmlNode>((IEnumerable<XmlNode>)provider.Data);
    }

    public class XmlNode : IDataErrorInfo
    {
        public string JobKey { get; set; }

        public XmlNode()
        {

        }

        public string Error
        {
            get { throw new NotImplementedException(); }
        }

        p开发者_StackOverflow中文版ublic string this[string columnName]
        {
            get
            {
                string result = string.Empty;
                if (columnName.Equals("JobKey"))
                {
                    if (JobKey.Where(s => Char.IsLetter(s)).Count() != JobKey.Length)
                        result = "Invalid name format. Name should contain letters only";
                }
             return result;
            }
        }
    }


Your implementation of Error in IDataErrorInfo is throwing an exception. The WPF binding mechanism is most likely calling this to determine if there are any instance-level errors, whereas the this[string columnName] indexer you have provided is returning property-level errors.

This...

public string Error
{
   get { return string.Empty; }
}

... would be a better alternative!

I could be wrong... but try breakpointing the throw new NotImplementedException() statement and see what happens!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜