-1

I'll try to explain this as succinctly as possible.

I have the following in my View:

  1. DataGrid

ItemsSource is bound to an ObservableDictionary<string, string> (that I got from Dr. WPF). SelectedItem is bound to a KeyValuePair property of my ViewModel. In the KeyValuePair's setter, it changes the value of a SelectedKey and SelectedValue property in the ViewModel appropriately.

  1. TextBox

Text is two-way bound to SelectedValue from above. In the SelectedValue's setter, it runs (simplified) ObservableDictionary[SelectedKey] = SelectedValue;

This allows the user to select a row, and change the value at any Key in the ObservableDictionary by typing in the TextBox. All of this functions correctly. Selecting a row and changing the TextBox text correctly changes the ObservableDictionary in code-behind, and the DataGrid itself updates in the View to reflect the new values.

However, any time a value is changed, the DataGrid gets a red border as though there is a validation error, and I cannot figure out why or how to find out what the validation error is. I have not set up any validation of any kind for the DataGrid, either in XAML or code-behind.

What can I do to find out the specific validation error so that I can try to fix it?

2
  • This is not off topic. There is a clear problem statement - How do I find out the specifics of a Validation Error? @AndrewBarber Oct 22, 2014 at 21:04
  • I think the confusion illustrated in the comments here and on the other answer seemed to suggest my choice of on-hold reason. That said, your posted answer also seems to validate what I thought the question was to begin with, so I'm going to reopen. Somehow, I didn't see your answer when I put this on-hold, even though you'd posted it a while before. Apologies for that! Oct 23, 2014 at 12:43

1 Answer 1

-1

After additional search, I was able to find my own answer. I bound a TextBox's Text property like so:

Text="{Binding ElementName=TheDataGrid, Path=(Validation.Errors)[0].ErrorContent, Mode=OneWay}"

With TheDataGrid being a name assigned to my DataGrid control.

5
  • @user1522548 How to get the validation error was the subject of the question, not the root cause of the problem. Downvoting my answer that correctly addresses my problem because you misunderstood the question is pretty rude. Oct 23, 2014 at 20:31
  • BTW what was the root cause of the validation errors now that you can see them?
    – JWP
    Oct 23, 2014 at 20:49
  • The SelectedItem -> KeyValuePair was throwing a conversion error. Oct 23, 2014 at 20:54
  • The error was non-specific and I didn't investigate any further. I just bound the SelectedItem to a different property type and removed the KeyValuePair. Oct 23, 2014 at 22:10
  • Ok, in our discussion yesterday, I was headed in the direction of the concept of Dictionary<string,string> binding. I tend to stay away from that type of binding because binding to intrinsic types presents problems not seen with strong types. One might say, well I have to have a Dictionary<string,string> whereas I would immediately thing about a List<ofMyClass> where OfMyClass simply has two string properties. The binding mechanisms work better with propriety names than with intrinsic types. Intrinsic types do not have property names...
    – JWP
    Oct 24, 2014 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.