/// <summary> /// Generates control fields /// </summary> /// <param name="source">The source.</param> /// <param name="classType">Type of the class.</param> /// <param name="initMethod">The initialize method.</param> /// <param name="generateField">if set to <c>true</c> [generate field].</param> /// <returns></returns> public override CodeExpression Generate(DependencyObject source, CodeTypeDeclaration classType, CodeMemberMethod initMethod, bool generateField) { CodeExpression fieldReference = base.Generate(source, classType, initMethod, generateField); DataGridBoundColumn column = source as DataGridBoundColumn; CodeComHelper.GenerateTemplateStyleField(classType, initMethod, fieldReference, source, DataGridBoundColumn.ElementStyleProperty, ColumnName + "_e"); Binding commandBindingExpr = column.Binding as Binding; if (commandBindingExpr != null) { GeneratedBindingsMode generatedMode = (GeneratedBindingsMode)source.GetValue(GeneratedBindings.ModeProperty); CodeVariableReferenceExpression bindingVar = CodeComHelper.GenerateBinding(initMethod, commandBindingExpr, ColumnName + "_b", generatedMode); var statement = new CodeAssignStatement(new CodeFieldReferenceExpression(fieldReference, "Binding"), bindingVar); initMethod.Statements.Add(statement); } return(fieldReference); }
/// <summary> /// Generates the binding path. /// </summary> /// <param name="propertyPath">The property path.</param> /// <param name="mode">The mode.</param> /// <returns></returns> /// <exception cref="System.ArgumentNullException">Namespace is empty.</exception> public bool GenerateBindingPath(PropertyPath propertyPath, GeneratedBindingsMode mode) { if (ActiveDataType == null || !IsEnabled) { return false; } if (ns == null) { throw new ArgumentNullException("Namespace is empty."); } Type dataType = ActiveDataType; if (dataType.IsPrimitive) { return false; } string path = propertyPath.Path; string[] pathParts = path.Split(partSeparator, StringSplitOptions.RemoveEmptyEntries); bool isCollectionProperty = false; for (int i = 0; i < pathParts.Length; i++) { string propertyName = pathParts[i]; int indexStart = propertyName.IndexOfAny(indexStartChars); if (indexStart != -1) { propertyName = propertyName.Substring(0, indexStart); } PropertyInfo propertyInfo = dataType.GetRuntimeProperty(propertyName); int paramStart = propertyName.IndexOfAny(paramStartChars); if (paramStart != -1) { int paramEnd = propertyName.IndexOfAny(paramEndChars); string paramIndexString = propertyName.Substring(paramStart + 1, paramEnd - paramStart - 1).Trim(); int paramIndex = Convert.ToInt32(paramIndexString); propertyInfo = propertyPath.PathParameters[paramIndex] as PropertyInfo; if (propertyInfo != null) { propertyName = propertyInfo.Name; dataType = propertyInfo.ReflectedType; } } string className = string.Format("{0}_{1}_PropertyInfo", dataType.Name, propertyName); className = Regex.Replace(className, "[`|<|>]", ""); CodeTypeDeclaration classType = CreateClass(className); if (propertyInfo == null) { // if mixed mode return with false (binding was not generated) but don't generate error so reflection can be used if (mode == GeneratedBindingsMode.Mixed) { return false; } CodeSnippetTypeMember error = new CodeSnippetTypeMember(string.Format("#error {0} type does not have property {1}", dataType, propertyName)); classType.Members.Add(error); ns.Types.Add(classType); return false; } isCollectionProperty = propertyInfo.PropertyType.IsArray || (typeof(IEnumerable).IsAssignableFrom(propertyInfo.PropertyType) && propertyInfo.PropertyType != typeof(string)); Type elementType = propertyInfo.PropertyType.GetElementType(); if (elementType == null && propertyInfo.PropertyType.IsGenericType) { elementType = propertyInfo.PropertyType.GetGenericArguments()[0]; } int key = propertyName.GetHashCode() ^ dataType.GetHashCode(); if (!generated.Contains(key)) { generated.Add(key); ns.Types.Add(classType); GenerateProperties(classType, propertyInfo); GenerateMethods(dataType, isCollectionProperty, propertyName, classType, propertyInfo, elementType); propertyInfos.Add(new Tuple<Type, string, string>(dataType, propertyName, ns.Name + "." + className)); } if (isCollectionProperty) { dataType = elementType; } else { dataType = propertyInfo.PropertyType; } } return true; }
/// <summary> /// Sets the mode. /// </summary> /// <param name="obj">The object.</param> /// <param name="value">The value.</param> public static void SetMode(DependencyObject obj, GeneratedBindingsMode value) { obj.SetValue(ModeProperty, value); }
/// <summary> /// Sets the mode. /// </summary> /// <param name="obj">The object.</param> /// <param name="value">The value.</param> public static void SetMode(DependencyObject obj, GeneratedBindingsMode value) { obj.SetValue(ModeProperty, value); }
/// <summary> /// Generates the binding path. /// </summary> /// <param name="propertyPath">The property path.</param> /// <param name="mode">The mode.</param> /// <returns></returns> /// <exception cref="System.ArgumentNullException">Namespace is empty.</exception> public bool GenerateBindingPath(PropertyPath propertyPath, GeneratedBindingsMode mode) { if (ActiveDataType == null || !IsEnabled) { return(false); } if (ns == null) { throw new ArgumentNullException("Namespace is empty."); } Type dataType = ActiveDataType; if (dataType.IsPrimitive) { return(false); } string path = propertyPath.Path; string[] pathParts = path.Split(partSeparator, StringSplitOptions.RemoveEmptyEntries); bool isCollectionProperty = false; for (int i = 0; i < pathParts.Length; i++) { string propertyName = pathParts[i]; int indexStart = propertyName.IndexOfAny(indexStartChars); if (indexStart != -1) { propertyName = propertyName.Substring(0, indexStart); } PropertyInfo propertyInfo = dataType.GetRuntimeProperty(propertyName); int paramStart = propertyName.IndexOfAny(paramStartChars); if (paramStart != -1) { int paramEnd = propertyName.IndexOfAny(paramEndChars); string paramIndexString = propertyName.Substring(paramStart + 1, paramEnd - paramStart - 1).Trim(); int paramIndex = Convert.ToInt32(paramIndexString); propertyInfo = propertyPath.PathParameters[paramIndex] as PropertyInfo; if (propertyInfo != null) { propertyName = propertyInfo.Name; dataType = propertyInfo.ReflectedType; } } string className = string.Format("{0}_{1}_PropertyInfo", dataType.Name, propertyName); className = Regex.Replace(className, "[`|<|>]", ""); CodeTypeDeclaration classType = CreateClass(className); if (propertyInfo == null) { // if mixed mode return with false (binding was not generated) but don't generate error so reflection can be used if (mode == GeneratedBindingsMode.Mixed) { return(false); } CodeSnippetTypeMember error = new CodeSnippetTypeMember(string.Format("#error {0} type does not have property {1}", dataType, propertyName)); classType.Members.Add(error); ns.Types.Add(classType); return(false); } isCollectionProperty = propertyInfo.PropertyType.IsArray || (typeof(IEnumerable).IsAssignableFrom(propertyInfo.PropertyType) && propertyInfo.PropertyType != typeof(string)); Type elementType = propertyInfo.PropertyType.GetElementType(); if (elementType == null && propertyInfo.PropertyType.IsGenericType) { elementType = propertyInfo.PropertyType.GetGenericArguments()[0]; } int key = propertyName.GetHashCode() ^ dataType.GetHashCode(); if (!generated.Contains(key)) { generated.Add(key); ns.Types.Add(classType); GenerateProperties(classType, propertyInfo); GenerateMethods(dataType, isCollectionProperty, propertyName, classType, propertyInfo, elementType); propertyInfos.Add(new Tuple <Type, string, string>(dataType, propertyName, ns.Name + "." + className)); } if (isCollectionProperty) { dataType = elementType; } else { dataType = propertyInfo.PropertyType; } } return(true); }