How to set the max length of PropertyDefinition

8 posts, 0 answers
  1. rui
    rui avatar
    46 posts
    Member since:
    May 2014

    Posted 19 Dec 2016 Link to this post

    How to set the max length of a PropertyDefinition  

    Like the textbox' s maxlength property

  2. Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    482 posts

    Posted 20 Dec 2016 Link to this post

    Hello rui,

    If you're using customized property definitions, you can manually set the MaxLength property of the definition's TextBox editor inside of the EditorTemplate. Additionally, you may want to have a look at the AutoBind Attached Behavior which enables you to use a single DataTemplate resource as an EditorTemplate value for multiple property definitions. You can also go over the respective demo from the SDK Samples Browser for a good example of its use.

    If you are, however, using autogenerated property definitions, you can get the list of property fields in RadPropertyGrid's Loaded event and customize any editors there. Here's an example:

    private void RadPropertyGrid_Loaded(object sender, RoutedEventArgs e)
    {
        var propertyGrid = sender as RadPropertyGrid;
        var propertyGridFields = propertyGrid.ChildrenOfType<PropertyGridField>();
        foreach (var field in propertyGridFields)
        {
            if ((field.DataContext as PropertyDefinition).AutoGeneratedPath == "StadiumCapacity")
            {
                var textBox = field.ChildrenOfType<TextBox>().First();
                if (textBox != null)
                {
                    textBox.MaxLength = 5;
                }
            }
        }
    }

    Please let me know if any of these approaches works for you.

    Regards,
    Dilyan Traykov
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  3. rui
    rui avatar
    46 posts
    Member since:
    May 2014

    Posted 21 Dec 2016 in reply to Dilyan Traykov Link to this post

    my  application has scores of PropertyGrid and hundred of  PropertyDefinition, So I need set maxlength by a simple way

    I wish to realize it by a attachproperty   and when setting

     public static readonly DependencyProperty MaxLength = DependencyProperty.RegisterAttached(
                "MaxLength", typeof(int), typeof(PropertyDefinitionHelper), new PropertyMetadata(300, OnMaxLengthChanged));

            private static void OnMaxLengthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                var propertyDefinition = d as PropertyDefinition;
                // Need to get textbox by PropertyDefinition
            }
           
            public static void SetMaxLength(DependencyObject dp, int value)
            {
                dp.SetValue(MaxLength, value);
            }

            public static int GetMaxLength(DependencyObject dp)
            {
                return (int)dp.GetValue(MaxLength);
            }

    xaml is  

       <telerik:RadPropertyGrid.PropertyDefinitions>

                    <telerik:PropertyDefinition Binding="{Binding Path=Name,
                                                                  Mode=TwoWay}"
                                                DisplayName="{x:Static localString:Resources.LblName}"
                                                GroupName="dataSquidPorperties"

    helper:PropertyDefinitionHelper.MaxLength = 300
                                                OrderIndex="0" />

     

     

     

     

  4. rui
    rui avatar
    46 posts
    Member since:
    May 2014

    Posted 21 Dec 2016 in reply to rui Link to this post

    there are two question

    1.the  OnMaxLengthChanged is not  trigger

    2.how to get textbox in template by PropertyDefinition

  5. Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    482 posts

    Posted 21 Dec 2016 Link to this post

    Hello rui,

    1) The reason OnMaxLengthChanged is not triggered is that the value you've set in XAML is the same one set as the property's default value. What you can do in this case is to simply omit the default value.

    2) You will need to handle RadPropertyGrid's Loaded event in order to ensure that all property fields have been created. You can then traverse them and set the MaxLength of the desired textboxes.

    Here's a modified version of the attached property which should result in the desired behavior:

    public class PropertyDefinitionHelper
    {
        public static readonly DependencyProperty MaxLength = DependencyProperty.RegisterAttached(
            "MaxLength", typeof(int), typeof(PropertyDefinitionHelper), new PropertyMetadata(OnMaxLengthChanged));
     
        private static void OnMaxLengthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var propertyGrid = d as RadPropertyGrid;
            propertyGrid.Loaded += PropertyGrid_Loaded;
        }
     
        private static void PropertyGrid_Loaded(object sender, RoutedEventArgs e)
        {
            var propertyGrid = sender as RadPropertyGrid;
            var propertyGridFields = propertyGrid.ChildrenOfType<PropertyGridField>();
            foreach (var field in propertyGridFields)
            {
                var textBoxes = field.ChildrenOfType<TextBox>();
     
                foreach (var textBox in textBoxes)
                {
                    textBox.MaxLength = (int) GetMaxLength(sender as DependencyObject);
                }
            }
        }
     
        public static void SetMaxLength(DependencyObject dp, int value)
        {
            dp.SetValue(MaxLength, value);
        }
     
        public static int GetMaxLength(DependencyObject dp)
        {
            return (int) dp.GetValue(MaxLength);
        }
    }

    Please let me know if this works for you.

    Regards,
    Dilyan Traykov
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  6. rui
    rui avatar
    46 posts
    Member since:
    May 2014

    Posted 21 Dec 2016 in reply to Dilyan Traykov Link to this post

    Thank you 

    but  our PropertyDefinition's maxlength need  set to different value by different field  

    so the attachedproperty should belong to PropertyDefinition Rather than PropertyGrid

    is  any solutinon for this ?

  7. Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    482 posts

    Posted 21 Dec 2016 Link to this post

    Hello rui,

    You can get ahold of the current property definition as well as the bound data item like so:

    private static void PropertyGrid_Loaded(object sender, RoutedEventArgs e)
    {
        var propertyGrid = sender as RadPropertyGrid;
        var propertyGridFields = propertyGrid.ChildrenOfType<PropertyGridField>();
        foreach (var field in propertyGridFields)
        {
            var propertyDefinition = field.DataContext;
            var textBoxes = field.ChildrenOfType<TextBox>();
     
            foreach (var textBox in textBoxes)
            {
                var dataItem = textBox.DataContext;
                // contionally set the MaxLength property
                textBox.MaxLength = (int) GetMaxLength(sender as DependencyObject);
            }
        }
    }

    Please let me know if this would work for you.

    Regards,
    Dilyan Traykov
    Telerik by Progress
    Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
  8. rui
    rui avatar
    46 posts
    Member since:
    May 2014

    Posted 23 Dec 2016 in reply to Dilyan Traykov Link to this post

    ok thank you
Back to Top