{"record":{"id":"d8a8a8bd2a565d89","repo":"dotnet/maui","slug":"expected-visualstategroup-but-found-parent-parent","errorCode":null,"errorMessage":"Expected VisualStateGroup but found {parent.Parent}","messagePattern":"Expected VisualStateGroup but found (.+?)","errorType":"exception","errorClass":"XamlParseException","httpStatus":null,"severity":"error","filePath":"src/Controls/src/Build.Tasks/CompiledConverters/BindablePropertyConverter.cs","lineNumber":89,"sourceCode":"\t\t\tif (bpRef == null)\n\t\t\t\tthrow new BuildException(PropertyResolution, node, null, propertyName, typeRef.Name);\n\t\t\treturn bpRef;\n\n\t\t\tstatic XmlType GetTargetTypeName(INode node)\n\t\t\t{\n\t\t\t\tvar targetType = ((node as ElementNode).Properties[new XmlName(\"\", \"TargetType\")] as ValueNode)?.Value as string;\n\t\t\t\treturn TypeArgumentsParser.ParseSingle(targetType, node.NamespaceResolver, (IXmlLineInfo)node);\n\t\t\t}\n\t\t}\n\n\t\tstatic XmlType FindTypeNameForVisualState(ElementNode parent, IXmlLineInfo lineInfo, ILContext context)\n\t\t{\n\t\t\t//1. parent is VisualState, don't check that\n\n\t\t\t//2. check that the VS is in a VSG\n\t\t\t// if (!(parent.Parent is IElementNode target) || target.XmlType.NamespaceUri != XamlParser.MauiUri || target.XmlType.Name != nameof(VisualStateGroup))\n\t\t\tif (parent.Parent is not ElementNode target || !target.XmlType.IsOfAnyType(nameof(VisualStateGroup)))\n\t\t\t\tthrow new XamlParseException($\"Expected {nameof(VisualStateGroup)} but found {parent.Parent}\", lineInfo);\n\n\t\t\t//3. if the VSG is in a VSGL, skip that as it could be implicit\n\t\t\tif (   target.Parent is ListNode\n\t\t\t\t|| target.Parent is ElementNode { XmlType: XmlType xt } && xt.IsOfAnyType(nameof(VisualStateGroupList)))\n\t\t\t\ttarget = target.Parent.Parent as ElementNode;\n\t\t\telse\n\t\t\t\ttarget = target.Parent as ElementNode;\n\n\t\t\t//4. target is now a Setter in a Style, or a VE\n\t\t\tif (target.XmlType.IsOfAnyType(nameof(Setter)))\n\t\t\t{\n\t\t\t\tvar targetType = ((target?.Parent as ElementNode)?.Properties[new XmlName(\"\", \"TargetType\")] as ValueNode)?.Value as string;\n\t\t\t\treturn TypeArgumentsParser.ParseSingle(targetType, parent.NamespaceResolver, lineInfo);\n\t\t\t}\n\t\t\telse\n\t\t\t\treturn target.XmlType;\n\t\t}\n","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/src/Build.Tasks/CompiledConverters/BindablePropertyConverter.cs#L71-L107","documentation":"A XamlParseException from FindTypeNameForVisualState. When resolving a single-name property inside a VisualState, the converter walks up the XAML tree to find the owning type and requires the VisualState's direct parent to be a VisualStateGroup. If that parent is any other element, it throws 'Expected VisualStateGroup but found ...'.","triggerScenarios":"A <VisualState> element is placed directly inside something other than a <VisualStateGroup> — e.g. directly under a VisualStateManager, a Style, or a control — so the parent check fails.","commonSituations":"Omitting the VisualStateGroup wrapper; mis-nesting VisualState inside VisualStateGroupList without the intermediate group; structural typo in a VisualStateManager block.","solutions":["Wrap <VisualState> elements inside a <VisualStateGroup>, which itself goes inside a <VisualStateGroupList> under VisualStateManager.VisualStates.","Validate the nesting order: control > VisualStateManager.VisualStates > VisualStateGroupList > VisualStateGroup > VisualState > Setter."],"exampleFix":"<!-- before -->\n<VisualStateManager.VisualStates>\n  <VisualState x:Name=\"Normal\">\n    <VisualState.Setters>\n      <Setter Property=\"BackgroundColor\" Value=\"Green\" />\n    </VisualState.Setters>\n  </VisualState>\n</VisualStateManager.VisualStates>\n\n<!-- after -->\n<VisualStateManager.VisualStates>\n  <VisualStateGroup>\n    <VisualState x:Name=\"Normal\">\n      <VisualState.Setters>\n        <Setter Property=\"BackgroundColor\" Value=\"Green\" />\n      </VisualState.Setters>\n    </VisualState>\n  </VisualStateGroup>\n</VisualStateManager.VisualStates>","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always nest VisualState inside a VisualStateGroup (and that inside VisualStateGroupList).","Memorize the hierarchy: VisualStateManager.VisualStates > VisualStateGroupList > VisualStateGroup > VisualState > Setter.","Use XAML templates/snippets for VisualStateManager blocks to avoid mis-nesting."],"tags":["xaml","xamlc","visualstate","compilation"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}