Example #1
0
 /// <summary>
 /// Loads a visual tree from the specified XAML, which will be placed under the parent. Leave null for no parent.
 /// </summary>
 public void LoadFromXAML(string xml, Element parent)
 {
     if (string.IsNullOrEmpty(xml))
         throw new ArgumentException(nameof(xml), "XAML must not be empty.");
     var doc = new XmlDocument();
     doc.LoadXml(xml);
     var nodes = doc.ChildNodes;
     LoadNode(nodes, parent);
 }
Example #2
0
 /// <summary>
 /// Create an instance of an element by its name.
 /// </summary>
 public Element CreateControlInstance(XmlNode node, Element parent)
 {
     // Add content (inline text) from the XML to a parent element.
     if (parent != null && node.Name.StartsWith("#text") && node.ParentNode != null)
         parent.AddContent(node.Value.Trim().TrimStart(newlineChars).TrimEnd(newlineChars));
     if (node.Name.StartsWith("#")) return null;
     var t = Type.GetType(typeof (Control).Namespace + '.' + node.Name);
     // Create a new element.
     return (Element) Activator.CreateInstance(t, manager);
 }
Example #3
0
        private void LoadNode(IEnumerable nodes, Element parent)
        {
            foreach (XmlNode node in nodes)
            {
                var control = CreateControlInstance(node, parent);
                if (control == null) continue;

                var props = TypeDescriptor.GetProperties(control.GetType());

                // Set attributes.
                if (node.Attributes != null)
                {
                    foreach (XmlAttribute xmlProperty in node.Attributes)
                    {
                        var propertyName = xmlProperty.Name;
                        var propertyDescriptor = props[propertyName];

                        if (propertyDescriptor != null)
                        {
                            object value = null;
                            // Convert the attribute to a value using a converter or Convert.ChangeType.
                            var found = false;
                            for (var i = 0; i < converters.Count; i++)
                            {
                                var converter = converters[i];
                                if (converter.CanConvert(propertyDescriptor.PropertyType))
                                {
                                    value = converter.Convert(propertyDescriptor.PropertyType, xmlProperty.Value);
                                    found = true;
                                    break;
                                }
                            }
                            if (!found)
                                value = Convert.ChangeType(xmlProperty.Value, propertyDescriptor.PropertyType);
                            propertyDescriptor.SetValue(control, value);
                        }
                    }
                }

                // Add element to parent or set as root element.
                if (parent == null)
                    manager.Elements.Add(control);
                else
                    parent.Add(control);
                LoadNode(node.ChildNodes, control);
            }
        }
Example #4
0
        public Window(Manager manager)
            : base(manager)
        {
            // TODO: Replace with control template.
            containerPanel = new StackPanel(manager);
            var background = (Color)"#323741";
            windowBorder =new Border(manager)
            {
                CornerRadius = 5,
                BorderThickness = 1,
                Background = Background,
                BorderBrush = background,
            };
            base.Add(windowBorder);
            titleBorder = new Border(manager)
            {
                Background = background,
                Padding = new Thickness(8, 12),
                HorizontalAlignment = HorizontalAlignment.Stretch,
                VerticalAlignment = VerticalAlignment.Top,
            };
            titleLabel = new TextBlock(manager, Title)
            {
                FontStyle = FontStyle.Bold,
                TextColor = Color.White,
                HorizontalAlignment = HorizontalAlignment.Left,
                TextAlignment = Alignment.TopLeft
            };
            windowBorder.Add(containerPanel);
            containerPanel.Add(titleBorder);
            titleBorder.Add(titleLabel);
            contentArea = new StackPanel(manager)
            {
                Margin = 8,
            };
            containerPanel.Add(contentArea);
            Margin = 16;

            Effects.Add(new DropShadowEffect(manager, 12, Color.Black * .2f));
        }
Example #5
0
        private void LoadNode(IEnumerable nodes, Element parent)
        {
            foreach (XmlNode node in nodes)
            {
                // Create an instance of the control.
                var control = CreateControlInstance(node, parent);
                if (control == null) continue;
                control.LogicalParent = parent;
                control.Parent = parent;

                if (node.Attributes != null)
                {
                    // Get all the dependency properties (and those inherited) of the control.
                    var props = DependencyProperty.GetProperties(control.GetType());
                    List<DependencyProperty> parentProps = null;

                    // For each xml property, try and find a dependency property to set.
                    foreach (XmlAttribute xmlProperty in node.Attributes)
                    {
                        var propertyName = xmlProperty.Name;

                        var prop = props.FirstOrDefault(p => p.Name == propertyName);
                        if (prop != null)
                        {
                            if (prop.Attached)
                                throw new InvalidOperationException("Attached property cannot be set on the parent.");
                            // Convert the string value to the dependency property type.
                            var value = ConvertValue(xmlProperty.Value, prop.ValueType);
                            control.SetValue(prop, value);
                        }
                        // Attached properties.
                        else if (propertyName.Contains(".") && parent != null)
                        {
                            if (parentProps == null) // Get parent properties only when needed and only once.
                                parentProps = DependencyProperty.GetProperties(parent.GetType());
                            var parts = propertyName.Split('.');
                            if (parts.Length == 2)
                            {
                                var parentName = parts[0];
                                propertyName = parts[1];
                                var parentType = parent.GetType();

                                if (parentType.Name == parentName)
                                {
                                    prop = parentProps.FirstOrDefault(p => p.Attached && p.Name == propertyName);
                                    if (prop != null)
                                    {
                                        var value = ConvertValue(xmlProperty.Value, prop.ValueType);
                                        control.SetValue(prop, value);
                                    }

                                }
                                else
                                    throw new InvalidOperationException("No parent found matching " + parentName);
                            }
                        }
                    }
                }

                // Add element to parent or set as root element.
                if (parent == null)
                    manager.AddRootElement(control);
                else
                    parent.Add(control);
                LoadNode(node.ChildNodes, control);
            }
        }
Example #6
0
 /// <summary>
 /// Remove a root level element from the UI.
 /// </summary>
 public void RemoveRootElement(Element element)
 {
     elements.Remove(element);
 }
Example #7
0
 /// <summary>
 /// Add an element directly and not to the presenter.
 /// </summary>
 internal virtual void AddDirect(Element element)
 {
     base.Add(element);
 }
Example #8
0
 public override void Add(Element element)
 {
     element.LogicalParent = LogicalParent;
     Presenter.Add(element);
 }
Example #9
0
 /// <summary>
 /// Remove an element from the arrange queue.
 /// </summary>
 public void RemoveArrange(Element element) => arrangeQueue.Remove(element);
Example #10
0
 /// <summary>
 /// Add an element to the measure queue.
 /// </summary>
 public void AddMeasure(Element element)
 {
     if (!measureQueue.Contains(element))
         measureQueue.Add(element);
 }
Example #11
0
 /// <summary>
 /// Add an element to the arrange queue.
 /// </summary>
 public void AddArrange(Element element)
 {
     if (!arrangeQueue.Contains(element))
         arrangeQueue.Add(element);
 }
Example #12
0
 /// <summary>
 /// Removes a child element from this element.
 /// </summary>
 /// <param name="element">The element to remove.</param>
 /// <param name="dispose">Should the control be disposed and destroyed?</param>
 public virtual void Remove(Element element, bool dispose = true)
 {
     if (element != null)
     {
         if (Elements.Contains(element))
         {
             Elements.Remove(element);
             // Element no longer has a parent.
             element.Parent = null;
             element.DependencyParent = null;
             if (dispose)
                 element.Dispose();
         }
     }
 }
Example #13
0
        /// <summary>
        /// Adds a child element to this element.
        /// </summary>
        /// <param name="element">The element to add.</param>
        public virtual void Add(Element element)
        {
            if (element.LogicalParent == null && LogicalParent != null)
                element.LogicalParent = LogicalParent;
            if (Elements.Count >= MaxChildren)
                throw new InvalidOperationException("This element may not have any more children. (Maximum: " +
                                                    MaxChildren + ") Try placing it inside a panel.");
            // If this control doesn't already contain the element.
            if (!Elements.Contains(element))
            {
                // Remove control from prior parent.
                element.Parent?.Remove(element);
                // Reset the level depth.
                element.Level = -1;

                element.Manager = Manager;
                element.Parent = this;

                // Add to this element.
                Elements.Add(element);
                element.DependencyParent = this;

                // Add to layout queue.
                Manager.Layout.AddMeasure(element);
                Manager.Layout.AddArrange(element);
            }
        }
Example #14
0
 private static void AddToTree(Element element, StringBuilder sb)
 {
     sb.Append(Environment.NewLine + new string(' ', element.Level * 3) +
               element.ToString().Remove(0, "Pyratron.UI.Controls.".Length));
     foreach (var child in element.Elements)
         AddToTree(child, sb);
 }
Example #15
0
 /// <summary>
 /// Remove an element from the measure queue.
 /// </summary>
 public void RemoveMeasure(Element element) => measureQueue.Remove(element);
Example #16
0
 public override void Add(Element element)
 {
     contentArea.Add(element);
 }
Example #17
0
 public void AddElement(Element element)
 {
     Elements.Add(element);
 }
Example #18
0
 public override void Remove(Element element, bool dispose = true)
 {
     Presenter.Remove(element, dispose);
 }
Example #19
0
 public override void Add(Element element)
 {
     groupsInvalidated = true;
     base.Add(element);
 }
Example #20
0
 /// <summary>
 /// Removes an element directly and not from the presenter.
 /// </summary>
 internal virtual void RemoveDirect(Element element)
 {
     base.Remove(element, false);
 }
Example #21
0
 /// <summary>
 /// Add a root level element to the UI.
 /// </summary>
 public void AddRootElement(Element element)
 {
     element.Level = 0;
     element.Parent = null;
     elements.Add(element);
 }