Skip to content

Improve Compiled Bindings + Context#999

Merged
wieslawsoltes merged 5 commits intomasterfrom
issue-973-docs-datatype
Jan 22, 2026
Merged

Improve Compiled Bindings + Context#999
wieslawsoltes merged 5 commits intomasterfrom
issue-973-docs-datatype

Conversation

@wieslawsoltes
Copy link
Owner

PR Summary: Issue 973 – Compiled Bindings + Context

Overview

This PR addresses the compiled binding errors reported in issue 973 by documenting the correct binding pattern and updating the ItemsSource sample to be compiled-binding-safe. It also adds [DataType] metadata to template-like classes (DocumentTemplate, Document, Tool) to align with Avalonia’s DataTemplate conventions for better design-time inference.

Changes Included

Documentation

  • Added a concise "compiled bindings + Context" note in dock-itemssource.md with a working pattern that rebases bindings onto the model via a nested DataContext.
  • Added a troubleshooting entry in dock-content-guide.md for compiled binding errors involving Context.*, explaining the cause and the recommended fix pattern.

Sample Update

  • Updated samples/DockXamlSample/ItemsSourceExample.axaml to:
    • Explicitly set x:DataType="dock:Document" on the template root.
    • Move x:DataType="local:MyDocumentModel" to a nested child under DataContext="{Binding Context}".
    • Bind model properties (Content, EditableContent, Status) under the nested model-typed subtree.

Metadata Improvements

Added [DataType] attributes to the following properties to enable Avalonia’s x:DataType inference and compiled binding metadata mapping:

  • Dock.Model.Avalonia.Controls.DocumentTemplate.DataType
  • Dock.Model.Avalonia.Controls.Document.DataType
  • Dock.Model.Avalonia.Controls.Tool.DataType

Why This Works

DocumentTemplate uses Document as its data context. Document.Context is typed object?, so compiled bindings can’t resolve Context.SomeProperty directly. The nested DataContext="{Binding Context}" pattern provides a strongly-typed subtree for compiled bindings. Adding [DataType] to template-like types aligns with Avalonia’s conventions and improves design-time and compile-time metadata resolution.

Usage Guidance

When using ItemsSource + DocumentTemplate with compiled bindings:

<DocumentTemplate>
  <StackPanel x:DataType="dock:Document">
    <TextBox Text="{Binding Title}" />
    <StackPanel DataContext="{Binding Context}">
      <StackPanel x:DataType="local:MyDocumentModel">
        <TextBox Text="{Binding Content}" />
      </StackPanel>
    </StackPanel>
  </StackPanel>
</DocumentTemplate>

This avoids compile-time errors like:

  • "Unable to resolve property 'Title' in data context of type 'ItemsSourceExampleViewModel'"
  • "Unable to resolve property 'Context' in data context of type 'MyDocumentModel'"

Files Touched

  • docfx/articles/dock-itemssource.md
  • docfx/articles/dock-content-guide.md
  • samples/DockXamlSample/ItemsSourceExample.axaml
  • src/Dock.Model.Avalonia/Controls/DocumentTemplate.cs
  • src/Dock.Model.Avalonia/Controls/Document.cs
  • src/Dock.Model.Avalonia/Controls/Tool.cs

Notes

These changes do not alter runtime behavior of Context binding; they document and formalize the correct compiled-binding pattern and improve metadata for tooling and compile-time validation.

Fixes #973

@wieslawsoltes wieslawsoltes merged commit 901bef3 into master Jan 22, 2026
4 checks passed
@wieslawsoltes wieslawsoltes deleted the issue-973-docs-datatype branch January 22, 2026 09:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DocumentTemplate Context property not passing through properties of model in ItemsSource setup

1 participant