unoplatform/uno · error · XamlGenerationException
Unable to find x:DataType in enclosing DataTemplate for x:Bi
Error message
Unable to find x:DataType in enclosing DataTemplate for x:Bind event
What it means
Thrown in the x:Bind event GetTargetType() logic when the binding is inside a DataTemplate (template.isInside) but the DataTemplate has no x:DataType attribute, and the path is not a static binding (does not start with a namespace prefix like 'local:'). x:Bind inside a DataTemplate is compiled and strongly typed, so it requires x:DataType to know the data type of each item; without it (and not being static) the generator cannot resolve the handler.
Source
Thrown at src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs:3833
INamedTypeSymbol GetTargetType()
{
if (template.isInside)
{
var dataTypeObject = FindMember(template.xamlObject!, "DataType", XamlConstants.XamlXmlNamespace);
if (dataTypeObject?.Value == null)
{
// Check if this is a static method binding (path starts with a namespace prefix like "local:Type")
// Static bindings don't require x:DataType because they don't access the DataContext
var pathParts = path.Split('.');
var isStaticBinding = pathParts.FirstOrDefault()?.Contains(":") ?? false;
if (isStaticBinding)
{
// Extract the type from the static path (e.g., "local:StaticHandler" from "local:StaticHandler.OnClick")
return GetType(RewriteNamespaces(pathParts[0]));
}
throw new XamlGenerationException("Unable to find x:DataType in enclosing DataTemplate for x:Bind event", bind);
}
return GetType(dataTypeObject.Value.ToString() ?? "");
}
else if (_xClassName?.Symbol is not null)
{
return _xClassName.Symbol;
}
else if (Generation.AutoGeneratedCodeBehindFiles.TryGetValue(_fileDefinition.UniqueID, out var autoGenBaseType))
{
// Use the XAML-derived base type when code-behind is auto-generated
return autoGenBaseType;
}
else
{
throw new XamlGenerationException($"Unable to find the type '{_xClassName?.Namespace}.{_xClassName?.ClassName}'", bind);
}
}View on GitHub (pinned to 0418340488)
Solutions
- Add x:DataType="local:MyItemViewModel" to the DataTemplate, matching the item type.
- If the handler is a static method, qualify the path with a namespace prefix, e.g. Click="{x:Bind local:Handlers.OnClick}" — this bypasses the x:DataType requirement.
- Move the handler to the page/code-behind and reference the page-level type instead of a template-scoped binding.
Example fix
<!-- before -->
<DataTemplate>
<Button Click="{x:Bind Delete}"/>
</DataTemplate>
<!-- after -->
<DataTemplate x:DataType="local:ItemViewModel">
<Button Click="{x:Bind Delete}"/>
</DataTemplate> Defensive patterns
Strategy: validation
Validate before calling
# Flag DataTemplate containing {x:Bind} without x:DataType (and not static)
Get-ChildItem -Recurse -Filter *.xaml | ForEach-Object {
$text = Get-Content $_.FullName -Raw
[regex]::Matches($text, '<DataTemplate(?![^>]*x:DataType)(.*?)</DataTemplate>', 'Singleline') |
ForEach-Object {
if ($_.Groups[1].Value -match '\{x:Bind(?!.*:)') {
Write-Warning "DataTemplate with x:Bind but no x:DataType (and non-static path)"
}
}
} Prevention
- Always set x:DataType on any DataTemplate that uses {x:Bind}.
- For static handlers, qualify the path with a namespace prefix (local:Type.Method) to bypass x:DataType.
- Remember x:Bind is compiled/strongly-typed and does not inherit DataContext like {Binding}.
When it happens
Trigger: Using {x:Bind SomeMethod} on an event inside a <DataTemplate> that has no x:DataType; a DataTemplate used for an ItemsControl where x:DataType was omitted.
Common situations: Adding x:Bind to a templated item without setting x:DataType; porting from {Binding} (late-bound) to {x:Bind} (compiled) and forgetting the type annotation; assuming DataContext propagates into templates for x:Bind (it does not — x:Bind uses x:DataType).
Related errors
- x:Bind event path cannot be empty
- Unable to find the type '{_xClassName?.Namespace}.{_xClassNa
- '{memberName}' is not defined on '{xamlObjectDefinition.Type
- Unable to find class name for toplevel control
- Unable to find x:Class on the top level element
AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13).
Data as JSON: /api/errors/352ca4e8baba69ff.
Report an issue: GitHub.