Validation

4 posts, 0 answers
  1. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 22 Apr 2013 Link to this post

    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
  2. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 22 Apr 2013 Link to this post

    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).
  3. UI for WPF is Visual Studio 2017 Ready
  4. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 23 Apr 2013 Link to this post

    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
  5. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1128 posts

    Posted 25 Apr 2013 Link to this post

    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.

Back to Top