7 Answers, 1 is accepted
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
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" />
there are two question
1.the OnMaxLengthChanged is not trigger
2.how to get textbox in template by PropertyDefinition
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
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 ?
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