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

How to set the max length of PropertyDefinition

7 Answers 394 Views
PropertyGrid
This is a migrated thread and some comments may be shown as answers.
rui
Top achievements
Rank 1
rui asked on 19 Dec 2016, 07:21 AM

How to set the max length of a PropertyDefinition  

Like the textbox' s maxlength property

7 Answers, 1 is accepted

Sort by
0
Dilyan Traykov
Telerik team
answered on 20 Dec 2016, 11:22 AM
Hello ,

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.
0
rui
Top achievements
Rank 1
answered on 21 Dec 2016, 08:13 AM

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" />

 

 

 

 

0
rui
Top achievements
Rank 1
answered on 21 Dec 2016, 08:33 AM

there are two question

1.the  OnMaxLengthChanged is not  trigger

2.how to get textbox in template by PropertyDefinition

0
Dilyan Traykov
Telerik team
answered on 21 Dec 2016, 12:18 PM
Hello ,

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.
0
rui
Top achievements
Rank 1
answered on 21 Dec 2016, 12:44 PM

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 ?

0
Dilyan Traykov
Telerik team
answered on 21 Dec 2016, 03:56 PM
Hello ,

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 you to write beautiful native mobile apps using a single shared C# codebase.
0
rui
Top achievements
Rank 1
answered on 23 Dec 2016, 09:30 AM
ok thank you
Tags
PropertyGrid
Asked by
rui
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
rui
Top achievements
Rank 1
Share this question
or