System.Windows.PropertyPathParser.Step C# (CSharp) Method

Step() public method

public Step ( string &typeName, string &propertyName, string &index ) : PropertyNodeType
typeName string
propertyName string
index string
return PropertyNodeType
		public PropertyNodeType Step (out string typeName, out string propertyName, out string index)
		{
			var type = PropertyNodeType.None;
			if (Path.Length == 0) {
				typeName = null;
				propertyName = null;
				index = null;
				return type;
			}

			int end;
			if (Path.StartsWith ("(")) {
				type = PropertyNodeType.AttachedProperty;
				end = Path.IndexOf (")");
				if (end == -1)
					throw new ArgumentException ("Invalid property path. Attached property is missing the closing bracket");

				int splitIndex = Path.IndexOf ('.', 0, end);
				if (splitIndex == -1)
					throw new Exception ("Invalid property path. Attached property is missing a '.'");

				typeName = Path.Substring (1, splitIndex - 1);
				propertyName = Path.Substring (splitIndex + 1, end - splitIndex - 1);
				index = null;
				Path = Path.Substring (end + 1);
			} else if (Path.StartsWith ("[")) {
				type = PropertyNodeType.Indexed;
				end = Path.IndexOf ("]");

				typeName = null;
				propertyName = null;
				index = Path.Substring (1, end - 1);
				Path = Path.Substring (end + 1);
				// You can do stuff like: [someIndex].SomeProp
				// as well as: [SomeIndex]SomeProp
				if (Path.StartsWith ("."))
					Path = Path.Substring (1);
			} else {
				type = PropertyNodeType.Property;
				end = Path.IndexOfAny (new char [] { '.', '[' });
				if (end == -1) {
					propertyName = Path;
					Path = "";
				}
				else {
					propertyName = Path.Substring (0, end);
					if (Path [end] == '.')
						Path = Path.Substring (end + 1);
					else
						Path = Path.Substring (end);
				}

				typeName = null;
				index = null;
			}

			return type;
		}
	}

Usage Example

Beispiel #1
0
		private static object ConvertPropertyPath (XamlTypeConverter converter, ITypeDescriptorContext context, CultureInfo culture, object value)
		{
			string typename;
			string propertyname;
			string index;
			PropertyNodeType node;
			string str = (string) value;

			// Fastpath - if there are no prefixed types then we have nothing to expand
			if (!str.Contains (":"))
				return new PropertyPath (str);

			var parser = new PropertyPathParser (str);
			var expanded = new StringBuilder ();
			while ((node = parser.Step (out typename, out propertyname, out index)) != PropertyNodeType.None) {
				switch (node) {
				case PropertyNodeType.AttachedProperty:
					if (expanded.Length > 0)
						expanded.Append ('.');

					if (typename.Contains (":")) {
						typename = converter.parser.ResolveType (typename).ToString ();
						expanded.AppendFormat ("('{0}'.{1})", typename, propertyname);
					} else {
						expanded.AppendFormat ("({0}.{1})", typename, propertyname);
					}
					break;
				case PropertyNodeType.Indexed:
					expanded.AppendFormat ("[{0}]", index);
					break;
				case PropertyNodeType.Property:
					if (expanded.Length > 0)
						expanded.Append ('.');

					expanded.Append (propertyname);
					break;
				default:
					throw new Exception (string.Format ("Could not handle PropertyNodeType.{0}", node));
				}
			}

			return new PropertyPath (str, expanded.ToString ());
		}
PropertyPathParser