This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
I'm using the 2.0 Property grid and seeing a potential bug with displaying validation errors in the property grid. Let's say I have a "Node" object that implements IDataErrorInfo. Let's also say I have a ReadOnlyCollection<Node> object and I've bound it to the property grid's SelectedObjects property. When the collection contains a single node, the property grid does query the node's Error property. However, if the Error property returns a string, the property grid does not display the expected red border around the node's invalid property. I assume that's because it's querying the Error property and not IDataErrorInfo's this[propName] property. Thus, I'm wondering if there's a way to enable displaying the desired red border when binding the grid to a collection of objects. Without it, my users won't know which property is invalid, even though the grid is only displaying a single element (as if I had used SelectedObject with a single item). While I could bind the collection to SelectedObject, I'm then stuck showing the node as a collection item, which I don't want. Note that I've also tried implementing IDataErrorInfo on the collection but that had no impact. Is there a way for me to workaround this problem? Thanks, -Craig |
|
|
I think this is probably occurring because, in a SelectedObjects scenario, the editors get bound to a Many object rather which proxies the underlying objects. Many doesn't currently implement IDataErrorInfo (because of the potential issues of trying to reconcile errors from across multiple objects), so the existence of IDataErrorInfo gets "lost" when your Node gets wrapped in the Many. Therefore the Binding.ValidatesOnDataErrors validation rule has nothing to look at, and the editor doesn't realise that there's an error. The fact that you're seeing queries to the Error property may mean I'm mistaken, but it might also just reflect the grid misguidedly trying to bind to that property for display purposes. A possible workaround, which will help with the single-element case, is to build a data source which intelligently switches between a "normal object" and a "multi-select" based on how many nodes are in the collection. To do this, you can use the SelectedObject property and the MultipleObjectWrapper class. I am thinking of something like this: public class OptionallyMultipleObjectConverter : IValueConverter <ms:PropertyGrid SelectedObject="{Binding Nodes, Converter={ ... }}" /> (where Nodes is the property that returns the ReadOnlyCollection<Node>). Obviously if you are setting the selection in code then you don't need to use the value converter, but I get the impression you are using data binding so I assume you want to keep this declarative. Note that we are binding SelectedObject (singular) not SelectedObjects. Setting SelectedObject to a MultipleObjectWrapper containing a collection of objects is the same as setting SelectedObjects to that collection of objects (in fact this is how SelectedObjects is implemented). Please note that (a) I haven't tested the above and (b) it won't help with showing validation errors when there are multiple elements in the collection. I don't think it will be possible to work around the latter case because of the Many/IDataErrorInfo issue noted above; we would be happy to look into providing a fix for this in the nightly builds, and would welcome your feedback on how you would expect it to behave if some selected objects were reporting errors for a property while others were not, and if different selected objects were reporting different error messages. |
|
|
Your suggestion worked great; thanks very much. |
|