PopupProperty.cs
- Last UpdatedMar 12, 2021
- 1 minute read
using System.Linq;
using Custom.Forms.Controls;
using Skelta.Forms2.Web;
namespace Kendo.Custom.ControlProperty
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using System.Xml;
using System.Xml.XPath;
using Skelta.Forms.Core.Controls;
using Skelta.Forms.Core.Interfaces;
/// <summary>
/// Class for showing the control property as Popup
/// </summary>
public class PopupProperty : IControlProperty
{
/// <summary>
/// Gets the Popup form control for rendering property
/// </summary>
/// <param name="mainControl">Main control</param>
/// <param name="propertyInformation">Property information</param>
/// <returns>Grid form with 2 text inputs</returns>
public BaseControl GetPropertyControl(BaseControl mainControl, PropertyInfo propertyInformation)
{
return new PropertyLookup
{
Id = "E_SetDataSource",
Name = propertyInformation.Name,
Description = propertyInformation.Name,
IsMandatory = true,
XmlNodeBoundTo = propertyInformation.Name,
ImageClass = "sknpl",
ClickFunction = "onIconClick"
};
}
/// <summary>
/// Read the property information from the XML document
/// </summary>
/// <param name="propertyInformation">Property information</param>
/// <param name="xmlDocument">Instance XML</param>
/// <param name="propertyValue">Value of the property</param>
public void ReadProperty(PropertyInfo propertyInformation, IXPathNavigable xmlDocument, object propertyValue)
{
var xmlDoc = xmlDocument as XmlDocument;
if (xmlDoc == null || propertyInformation == null || propertyValue == null)
{
return;
}
if (string.IsNullOrEmpty(propertyValue.ToString())) return;
var propElement = xmlDoc.CreateElement(propertyInformation.Name);
propElement.InnerText = propertyValue.ToString();
xmlDoc.FirstChild.AppendChild(propElement);
}
/// <summary>
/// Set the value to property of the control
/// </summary>
/// <param name="xmlDocument">Instance XML</param>
/// <param name="propertyControl">Control which is rendered for the property</param>
/// <param name="mainControl">Main control</param>
public void WriteProperty(IXPathNavigable xmlDocument, BaseDataControl propertyControl, BaseControl mainControl)
{
var xmlDoc = xmlDocument as XmlNode;
if (xmlDoc == null || propertyControl == null || mainControl == null)
{
return;
}
var value = string.Empty;
foreach (var item in xmlDoc.FirstChild.ChildNodes.Cast<XmlNode>().Where(item => item.Name == propertyControl.XmlNodeBoundTo))
{
value = item.InnerText;
}
var prop = mainControl.GetType().GetProperty(propertyControl.XmlNodeBoundTo);
prop.SetValue(mainControl, Convert.ChangeType(value, prop.PropertyType, CultureInfo.InvariantCulture), null);
}
}
}