dotnet/maui · error · FormatException
Path contains an empty part
Error message
Path contains an empty part
What it means
Thrown while parsing the binding path string when, after splitting on '.' and trimming, a path part is the empty string. This indicates a malformed path such as leading/trailing/consecutive dots (e.g. "A..B", ".A", "A.").
Source
Thrown at src/Controls/src/Core/BindingExpression.cs:235
string p = Path.Trim();
var last = new BindingExpressionPart(this, ".");
_parts.Add(last);
if (p[0] == '.')
{
if (p.Length == 1)
return;
p = p.Substring(1);
}
string[] pathParts = p.Split(ExpressionSplit);
for (var i = 0; i < pathParts.Length; i++)
{
string part = pathParts[i].Trim();
if (part == string.Empty)
throw new FormatException("Path contains an empty part");
BindingExpressionPart indexer = null;
int lbIndex = part.IndexOf("[", StringComparison.Ordinal);
if (lbIndex != -1)
{
int rbIndex = part.Length - 1;
if (part[rbIndex] != ']')
throw new FormatException("Indexer did not contain closing bracket");
int argLength = rbIndex - lbIndex - 1;
if (argLength == 0)
throw new FormatException("Indexer did not contain arguments");
string argString = part.Substring(lbIndex + 1, argLength);
indexer = new BindingExpressionPart(this, argString, true);
part = part.Substring(0, lbIndex);View on GitHub (pinned to f377ff1c5e)
Solutions
- Sanitize the path: remove empty segments and leading/trailing dots before constructing the Binding.
- Build paths with a helper that joins non-empty parts with '.'.
- Validate path tokens against a property whitelist and reject malformed input early.
Example fix
// before
var path = $"{parent}.{child}"; // child is "" -> "Parent."
var b = new Binding(path);
// after
var parts = new[] { parent, child }.Where(s => !string.IsNullOrWhiteSpace(s));
var path = string.Join(".", parts);
var b = new Binding(path); Defensive patterns
Strategy: validation
Validate before calling
static string SanitizePath(string raw)
{
var parts = raw.Split('.').Where(p => !string.IsNullOrWhiteSpace(p));
return string.Join(".", parts);
}
var binding = new Binding(SanitizePath(rawPath)); Type guard
static bool IsValidBindingPath(string p) =>
!string.IsNullOrWhiteSpace(p) &&
p.Split('.').All(seg => !string.IsNullOrWhiteSpace(seg)); Prevention
- Build paths by joining non-empty segments with '.'.
- Reject user/config paths with consecutive or leading/trailing dots.
- Unit-test dynamic path construction.
When it happens
Trigger: Constructing new Binding("A..B"), new Binding(".Name"), new Binding("Name."), or any path where Split('.') yields an empty token. Dynamically building path strings with string concatenation that leaves stray dots.
Common situations: Building paths from nullable/optional segments (string.Format("{0}.{1}") where a segment is empty). Reading paths from config/JSON with trailing dots. User input feeding a binding path.
Related errors
- Indexer did not contain closing bracket
- Indexer did not contain arguments
- mode is not a valid BindingMode
- RootComponent requires a value for its Selector property, bu
- RootComponent requires a value for its ComponentType propert
AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13).
Data as JSON: /api/errors/92cca697a76d1d24.
Report an issue: GitHub.