Example #1
0
        RenderFragment RenderChildren(IEnumerable children, int depth)
        {
            var level = depth < Levels.Count() ? Levels.ElementAt(depth) : Levels.Last();

            return(new RenderFragment(builder =>
            {
                Func <object, string> text = null;

                foreach (var data in children)
                {
                    if (text == null)
                    {
                        text = level.Text ?? Getter <string>(data, level.TextProperty);
                    }

                    RenderTreeItem(builder, data, level.Template, text, level.HasChildren, level.Expanded, level.Selected);

                    var hasChildren = level.HasChildren(data);

                    if (!string.IsNullOrEmpty(level.ChildrenProperty))
                    {
                        var grandChildren = PropertyAccess.GetValue(data, level.ChildrenProperty) as IEnumerable;

                        if (grandChildren != null && hasChildren)
                        {
                            builder.AddAttribute(7, "ChildContent", RenderChildren(grandChildren, depth + 1));
                            builder.AddAttribute(8, nameof(RadzenTreeItem.Data), grandChildren);
                        }
                    }

                    builder.CloseComponent();
                }
            }));
        }
Example #2
0
            public dynamic GetValueFromScope(string propertyName)
            {
                try
                {
                    var keys  = propertyName.Split('.');
                    var digTo = keys.Count(x => x == "$parent");
                    var d     = 0;
                    var p     = this;

                    while (digTo > 0 && digTo + 1 > d)
                    {
                        p  = p.Parent;
                        d += p.Scope != null ? 1 : 0;
                    }
                    if (digTo > 0)
                    {
                        keys = keys.Where(x => x != "$parent").ToArray();
                    }

                    var property = new PropertyAccess(keys[0]);
                    var scope    = p.Scope;
                    var parent   = this;
                    while (scope == null || !scope.ContainsKey(property.Name))
                    {
                        parent = parent.Parent;
                        scope  = parent.Scope;
                    }

                    var obj   = scope[property.Name];
                    var level = 1;

                    while (level < keys.Length)
                    {
                        obj      = property.GetValue(obj);
                        property = new PropertyAccess(keys[level]);
                        var t = obj.GetType();
                        obj = t == typeof(Dictionary <string, dynamic>) || t.IsArray
                            ? obj[property.Name]
                            : t.GetProperty(property.Name).GetValue(obj, null);
                        level++;
                    }

                    return(property.GetValue(obj));
                }
                catch (Exception)
                {
                    Trace.WriteLine(propertyName + " not found. default value returned = false");
                    return(false);
                }
            }
Example #3
0
        /// <summary>
        /// Gets the value for specified item.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns>System.Object.</returns>
        public object GetValue(TItem item)
        {
            var value = propertyValueGetter != null && !string.IsNullOrEmpty(Property) && !Property.Contains('.') ? propertyValueGetter(item) : !string.IsNullOrEmpty(Property) ? PropertyAccess.GetValue(item, Property) : "";

            return(!string.IsNullOrEmpty(FormatString) ? string.Format(FormatString, value, Grid?.Culture ?? CultureInfo.CurrentCulture) : Convert.ToString(value, Grid?.Culture ?? CultureInfo.CurrentCulture));
        }
Example #4
0
            public dynamic GetValueFromScope(string propertyName, string format = null)
            {
                try
                {
                    var keys = propertyName.Split('.');
                    var digTo = keys.Count(x => x == "$parent");
                    var d = 0;
                    var p = this;

                    while (digTo > 0 && digTo + 1 > d)
                    {
                        p = p.Parent;
                        d += p.Scope != null ? 1 : 0;
                    }
                    if (digTo > 0) keys = keys.Where(x => x != "$parent").ToArray();

                    var property = new PropertyAccess(keys[0]);
                    var scope = p.Scope;
                    var parent = this;
                    while (scope == null || !scope.ContainsKey(property.Name))
                    {
                        parent = parent.Parent;
                        scope = parent.Scope;
                    }

                    var obj = scope[property.Name];
                    var level = 1;

                    while (level < keys.Length)
                    {
                        obj = property.GetValue(obj);
                        property = new PropertyAccess(keys[level]);
                        var t = obj.GetType();
                        obj = t == typeof(Dictionary<string, dynamic>) || t.IsArray
                            ? obj[property.Name]
                            : t.GetProperty(property.Name).GetValue(obj, null);
                        level++;
                    }

                    if (string.IsNullOrWhiteSpace(format))
                        return property.GetValue(obj);
                    return property.GetValue(obj).ToString(format);
                }
                catch (Exception)
                {
                    Trace.WriteLine(propertyName + " not found. default value returned = false");
                    return false;
                }
            }
Example #5
0
        public static void SetModel(object viewObj, Control parentControl = null, bool asState = false)
        {
            if (parentControl == null)
            {
                parentControl = Current;
            }
            var list = GetPostPair(viewObj, parentControl);

            if (list.Count == 0)
            {
                throw new InvalidOperationException("PostPair's empty!");
            }

            Page page = parentControl.Page;

            foreach (PostPair pair in list)
            {
                Control control = page.FindControl(pair.UniqueID);
                if (control == null)
                {
                    throw new InvalidOperationException(string.Format("SetPost:'{0}' isn't exists", pair.UniqueID));
                }

                PropertyAccess property = pair.Property;
                object         value    = property.GetValue(viewObj);
                Type           propType = property.EntityProperty.PropertyType;
                if (!TypeHelper.IsStringOrValueType(propType))
                {
                    if (value != null)
                    {
                        if (value is System.Collections.IEnumerable)
                        {
                            BindData(control, value);
                        }
                        else
                        {
                            SetModel(value, parentControl);
                        }
                    }
                    continue;
                }

                Type underlyingType;
                if (TypeHelper.IsNullableType(propType, out underlyingType))
                {
                    propType = underlyingType;
                }
                if (propType.IsEnum)
                {
                    if (value == null)
                    {
                        continue;
                    }

                    value = Convert.ChangeType(value, typeof(int));

                    ListControl listControl = control as ListControl;
                    if (listControl != null)
                    {
                        if (listControl.Items.Count == 0)
                        {
                            BindEnum(listControl, propType);
                        }
                        if ((listControl is CheckBoxList) && Attribute.IsDefined(propType, typeof(FlagsAttribute)))
                        {
                            int flag = (int)value;
                            foreach (ListItem item in listControl.Items)
                            {
                                int itemValue = int.Parse(item.Value);
                                if ((flag & itemValue) == itemValue)
                                {
                                    item.Selected = true;
                                }
                            }
                            continue;
                        }
                    }
                }
                if (value == null)
                {
                    value = string.Empty;
                }
                ITextControl textControl = control as ITextControl;
                if (textControl != null)
                {
                    textControl.Text = value.ToString();
                }
                else
                {
                    ICheckBoxControl checkBoxControl = control as ICheckBoxControl;
                    if (checkBoxControl != null)
                    {
                        checkBoxControl.Checked = Convert.ToBoolean(value);
                    }
                    else
                    {
                        HiddenField hiddenField = control as HiddenField;
                        if (hiddenField != null)
                        {
                            hiddenField.Value = value.ToString();
                        }
                    }
                }
            }
            if (asState)
            {
                var dict = (ListDictionary)page.Session["ModelState"];
                if (dict == null)
                {
                    page.Session["ModelState"] = dict = new ListDictionary();
                }
                dict[viewObj.GetType()] = viewObj;
            }
        }