Quantcast
Channel: Telerik Forums RSS
Viewing all articles
Browse latest Browse all 94857

Custom Merge Field: How to pass a new DisplayName property into the CodeFragment (instead of Property Path)?

$
0
0

 I'm trying to replace the Code view of PropertyPath in {MF PropertyPath} to {MF DisplayName}, which still uses PropertyPath inside.

[XamlSerializable]
publicstringDisplayName
{
    get{ return_displayName.GetValue(); }
 
    set
    {
        if(!_displayName.IsNestedField && _displayName.GetValue() == value)
            return;
 
        _displayName.SetValue(value);
        InvalidateCode();
    }
}
 
[XamlSerializable]
publicstringPropertyPath
{
    get
    {
        return_propertyPath.GetValue();
    }
 
    set
    {
        if(!_propertyPath.IsNestedField && _propertyPath.GetValue() == value)
            return;
 
        _propertyPath.SetValue(value);
        InvalidateCode();
    }
}
 

 They XamlSerializable, then there should be no problems.

 But there is a problem with the implementation of CopyPropertiesFromCodeExpression.

 

protectedoverridevoidCopyPropertiesFromCodeExpression(FieldCodeExpression fieldCodeExpression)
{
    if(fieldCodeExpression.FieldArgumentNode != null)
    {
        _propertyPath.SetValue(fieldCodeExpression.FieldArgumentNode);
        _displayName.SetValue(fieldCodeExpression.FieldArgumentNode);
    }
    else
    {
        PropertyPath = null;
        DisplayName = null;
    }
}

 

I get a blank page without this method. Otherwise, it tries to substitute the DisplayName in PropertyPath (???).

 Full Code:

publicclassEnchantedMergeField : CodeBasedField
{
    publicstaticreadonlystringFieldType = "MF";
    publicstaticreadonlyFieldPropertyDefinition PropertyPathProperty = newFieldPropertyDefinition("PropertyPath");
    publicstaticreadonlyFieldPropertyDefinition DisplayNameProperty = newFieldPropertyDefinition("DisplayName");
 
    privatereadonlyFieldProperty _propertyPath;
    privatereadonlyFieldProperty _displayName;
 
    staticEnchantedMergeField()
    {
        CodeBasedFieldFactory.RegisterFieldType(FieldType, () => newEnchantedMergeField());
    }
 
    publicEnchantedMergeField()
    {
        _propertyPath = newFieldProperty(this, PropertyPathProperty);
        _displayName = newFieldProperty(this, DisplayNameProperty);
    }
 
    publicoverridestringFieldTypeName
    {
        get{ returnFieldType; }
    }
 
    publicoverrideboolHasDisplayNameFragment
    {
        get
        {
            returntrue;
        }
    }
 
    [XamlSerializable]
    publicstringDisplayName
    {
        get{ return_displayName.GetValue(); }
 
        set
        {
            if(!_displayName.IsNestedField && _displayName.GetValue() == value)
                return;
 
            _displayName.SetValue(value);
            InvalidateCode();
        }
    }
 
    [XamlSerializable]
    publicstringPropertyPath
    {
        get
        {
            return_propertyPath.GetValue();
        }
 
        set
        {
            if(!_propertyPath.IsNestedField && _propertyPath.GetValue() == value)
                return;
 
            _propertyPath.SetValue(value);
            InvalidateCode();
        }
    }
 
    protectedoverridevoidCopyPropertiesFromCodeExpression(FieldCodeExpression fieldCodeExpression)
    {
        if(fieldCodeExpression.FieldArgumentNode != null)
        {
            _propertyPath.SetValue(fieldCodeExpression.FieldArgumentNode);
            _displayName.SetValue(fieldCodeExpression.FieldArgumentNode);
        }
        else
        {
            PropertyPath = null;
            DisplayName = null;
        }
    }
 
    publicoverridevoidCopyPropertiesFrom(Field fromField)
    {
        base.CopyPropertiesFrom(fromField);
 
        var mergeField = (EnchantedMergeField) fromField;
 
        _propertyPath.CopyPropertiesFrom(mergeField._propertyPath);
        _displayName.CopyPropertiesFrom(mergeField._displayName);
    }
 
    publicoverrideField CreateInstance()
    {
        returnnewEnchantedMergeField();
    }
 
    privatestringConvertToString(objectvalue)
    {
        stringconvertedValue;
 
        if(value isstring)
            convertedValue = (string) value;
        elseif(value isDateTime)
            convertedValue = ((DateTime) value).ToString(DateTimeFormatting);
        else
        {
            try
            {
                convertedValue = Convert.ToDecimal(value).ToString(NumericFormatting);
            }
            catch(Exception)
            {
                convertedValue = value.ToString();
            }
        }
 
        returnconvertedValue;
    }
 
    protectedoverrideDocumentFragment GetResultFragment()
    {
        stringvalue = null;
 
        if(Document != null&& !string.IsNullOrEmpty(PropertyPath))
        {
            var currentItem = Document.MailMergeDataSource.CurrentItem;
 
            if(currentItem != null)
            {
                value = ConvertToString(GetNestedPropertyValue(currentItem, PropertyPath));
            }
        }
 
        returnCreateFragmentFromText(value);
    }
 
    protectedoverridevoidBuildCodeOverride()
    {
        CodeBuilder.SetFieldArgument(_propertyPath);
    }
 
    protectedoverrideDocumentFragment GetCodeFragment()
    {
        returnCreateFragmentFromText(string.Format("{{{0} {1}}}", FieldType, DisplayName));
    }
 
    protectedoverrideDocumentFragment GetDisplayNameFragment()
    {
        returnCreateFragmentFromText(string.Format(DisplayNameFragmentFormat, DisplayName));
    }
 
    privatestaticobjectGetNestedPropertyValue(objectvalue, stringpropertyPath)
    {
        if(value == null|| string.IsNullOrEmpty(propertyPath))
            returnnull;
 
        var currentType = value.GetType();
        var path = propertyPath.Split('.');
 
        foreach(var propertyStep inpath)
        {
            var prop = currentType.GetProperty(propertyStep);
 
            if(prop == null|| value == null)
            {
                value = "#{ERROR}#";
                break;
            }
 
            value = prop.GetValue(value, null);
            currentType = prop.PropertyType;
        }
 
        returnvalue;
    }
}

 


Viewing all articles
Browse latest Browse all 94857

Latest Images

Trending Articles



Latest Images