After starting to write this tip/trick, I realized I don't actually need this code, but I figure since I spent all morning on it, it's a shame to delete it without giving someone else a chance to laugh at me, so here it is.
----------------------
I've been working with the Silverlight4
RichTextBox
, and it has an interesting property called
Xaml
. This property returns a string that represents the xaml code used to build the content of the
RichTextBox
. Also interesting is that no other control has such a property, yet in order to display certain components in a
RichTextBox
, you need the Xaml to display it as content. One of the components I need to display is a multicolumn
Grid
.
The grid is built in a child window in a
ListBox
(this is the point at which I realized I didn't need the code in this tip), and when the user clicks the OK button, the child window iterates through the
ListBox
items (sort of), and builds the Xaml necessary to display the
ListBox
with it's items. As you can probably guess by now, I don't need the items contained by a
ListBox
, I need them in a multicolumn grid. But I digress from my prior digression.
Essentially, I build a collection of xaml elements with their attributes and child elements, and when the time is right, I call the
GetXaml()
method. (And this is where I realized the whole thing was a waste of time because I could have just saved a collection of strings that represented the xaml in question.)
So, here you see a completely worthless lump of code that I spent about four hours on, that is going to be deleted from our project. I guess the tip here is not to be afraid to admit when you've wasted a whole morning on code that is not only over-complicated, but isn't even necessary. I've been coding for 30 years, and I still do this kind of stuff.
So, for your amusement, I present the most worthless code in the universe (and only because I'd spent about 10 minutes formatting it for CP).
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Text;
public class AttributeCollection : Dictionary<string, string>
{
}
public class ElementCollection : List<XamlElement>
{
}
public class XamlElement
{
private string m_name = "";
private string m_value = "";
private ElementCollection m_children = new ElementCollection();
private AttributeCollection m_attributes = new AttributeCollection();
public string Name
{
get { return m_name; }
set { m_name = value; }
}
public string Value
{
get { return m_value; }
set { m_value = value; }
}
public ElementCollection Children
{
get { return m_children; }
set { m_children = value; }
}
public AttributeCollection Attributes
{
get { return m_attributes; }
set { m_attributes = value; }
}
public XamlElement()
{
}
public XamlElement(string nameValue)
{
this.Name = nameValue;
}
public XamlElement(string nameValue, string text)
{
this.Name = nameValue;
this.Value = text;
}
public void AddAttribute(string key, string value)
{
if ((this.Attributes == null))
{
this.Attributes = new AttributeCollection();
}
this.Attributes.Add(key, value);
}
public void AddAttribute(KeyValuePair<string, string> keyValue)
{
if ((this.Attributes == null))
{
this.Attributes = new AttributeCollection();
}
this.Attributes.Add(keyValue.Key, keyValue.Value);
}
public void AddChild(XamlElement element)
{
if ((this.Children == null))
{
this.Children = new ElementCollection();
}
this.Children.Add(element);
}
public void AddTextContent(string text)
{
if ((this.Children == null))
{
this.Children = new ElementCollection();
}
this.Children.Clear();
this.Value = text;
}
public string GetXaml()
{
StringBuilder xaml = new StringBuilder("");
xaml.AppendFormat("<{0}", this.Name);
foreach (KeyValuePair<string, string> attribute in this.Attributes)
{
xaml.AppendFormat(" {0}=\"{1}\"", attribute.Key, attribute.Value);
}
if (this.Children.Count > 0)
{
xaml.Append(">");
foreach (XamlElement child in this.Children)
{
xaml.Append(child.GetXaml());
}
xaml.AppendFormat("</{0}>", this.Name);
}
else if (!string.IsNullOrEmpty(this.Value))
{
xaml.AppendFormat(">{0}</{1}>", this.Value, this.Name);
}
else
{
xaml.Append("/>");
}
return xaml.ToString();
}
}