This is a migrated thread and some comments may be shown as answers.

Validation

3 Answers 125 Views
PropertyGrid
This is a migrated thread and some comments may be shown as answers.
ManniAT
Top achievements
Rank 2
ManniAT asked on 22 Apr 2013, 08:25 PM
Hi,

I bind a PropertyGrid to a pretty complex object generated by Linq2SQL.
I implemented IDataErrorInfo on the object.
Further let the PropertyGrid autogenerate the properties.

Now I have two Problems - this[string ColumnName] never gets called.
Next - the property Grid tries to bind "Error" (I know I can fix this in code behind - but this would break my so far "no code behind" MVVM design) - but this isn't very important.
Last not least I implemented "IsValid" like in the following snippet.
    public static bool IsValid(this DependencyObject obj) {
            // The dependency object is valid if it has no errors, 
            //and all of its children (that are dependency objects) are error-free.
            return !Validation.GetHasError(obj) &&
                GetVisualTreeChildren(obj)
                .OfType<DependencyObject>()
                .All(child => IsValid(child));
        }
        //VisualTreeHelper don't have a method to get all the children of a visual object
        private static IEnumerable GetVisualTreeChildren(DependencyObject parent) {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
                yield return VisualTreeHelper.GetChild(parent, i);
        }
 
 
//OK handler in VM using the IsValid extension
    protected override bool HandleOKCanExecute(object arg) {
            if (this.TheView != null) {
                return(this.TheView.IsValid());
                }
            return (false);            
        }

I further know that I could decorate my object with attributes - but the reason to use your PropertyGrid is that the object is large (a lot of properties) and I don't want to define all these things. And it would be hard without touching the Linq2Sql autogenerated code.

Main Problem - no validation (this[string ColumnName] occurs.

Manfred

3 Answers, 1 is accepted

Sort by
0
ManniAT
Top achievements
Rank 2
answered on 22 Apr 2013, 08:31 PM
I just tried the sample (last post) from this thread.
http://www.telerik.com/community/forums/wpf/property-grid/validation.aspx

Also no validation....

I use version 2013.1.408.45 (FW 4.5).
0
ManniAT
Top achievements
Rank 2
answered on 23 Apr 2013, 07:57 AM
Hi,

I could manage it by creating my own class...
    public class XPropertyGrid : RadPropertyGrid {
 
 
        #region HandleIDataErrorInfo handling
        public bool HandleIDataErrorInfo {
            get { return (bool)GetValue(HandleIDataErrorInfoProperty); }
            set { SetValue(HandleIDataErrorInfoProperty, value); }
        }
        // Using a DependencyProperty as the backing store for HandleIDataErrorInfo.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HandleIDataErrorInfoProperty =
            DependencyProperty.Register("HandleIDataErrorInfo", typeof(bool), typeof(XPropertyGrid),
                new FrameworkPropertyMetadata(true));
        #endregion
 
        public XPropertyGrid() {
            AutoGeneratingPropertyDefinition += XPropertyGrid_AutoGeneratingPropertyDefinition;
        }
 
        void XPropertyGrid_AutoGeneratingPropertyDefinition(object sender, global::Telerik.Windows.Controls.Data.PropertyGrid.AutoGeneratingPropertyDefinitionEventArgs e) {
            if (!HandleIDataErrorInfo) {
                return;
            }
            if (e.PropertyDefinition.DisplayName == "Error") {
                e.Cancel = true;
            }
            Binding bnD = e.PropertyDefinition.Binding as Binding;
            if (bnD != null) {
                bnD.ValidatesOnDataErrors = true;
                bnD.ValidatesOnExceptions = true;
            }
        }
    }
}


Manfred
0
Ivan Ivanov
Telerik team
answered on 25 Apr 2013, 02:42 PM
Hi Manfred,

 Please excuse me for the delayed reply. Basically, RadPropertyGrid support IDataErrorInfo, but the default binding generated for auto-generated PropertyDefinition does not have the ValidatesOnDataErrors and NotifyOnValidationError properties set to true. I Guess that you should be able to handle this without creating a new class, just subscribe to the auto-generating event and set the properties to true there.

All the best,
Ivan Ivanov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
PropertyGrid
Asked by
ManniAT
Top achievements
Rank 2
Answers by
ManniAT
Top achievements
Rank 2
Ivan Ivanov
Telerik team
Share this question
or