Skip to content

[Fix] #505 - [LayoutItem] Let the CanClose style setter win over the … - #633

Merged
sharpSteff merged 2 commits into
Dirkster99:masterfrom
sharpSteff:fix/505-canclose-style-setter
Aug 8, 2026
Merged

sharpSteff merged 2 commits into
Dirkster99:masterfrom
sharpSteff:fix/505-canclose-style-setter

Conversation

@sharpSteff

Copy link
Copy Markdown
Collaborator

Closes #505

The bug

LayoutItem.SetDefaultBindings() seeded CanClose from the layout model with a plain assignment:

CanClose = LayoutElement.CanClose;   // stamps a LOCAL value

A local value outranks a Style setter in WPF's precedence order, so
<Setter Property="CanClose" Value="False"/> was silently discarded whenever the container style
arrived after the item already existed.

Which of the two happens first is decided by where the DataContext is assigned — which is exactly
why the issue reports it as a DataContext problem:

  • code-behind (works): DocumentsSource resolves after LayoutItemContainerStyle(Selector) has
    been set, so the style setter applies to a fresh item and the trailing sync merely re-writes the
    same false.
  • <Window.DataContext> in XAML (broken): the binding resolves during parsing, so the documents
    — and their LayoutItems — exist before the parser reaches the style property. The local true
    is already in place and the setter never takes effect. The close button stays visible.

The fix

One line in Controls/LayoutItem.cs — seed the value with SetCurrentValue instead:

SetCurrentValue(CanCloseProperty, LayoutElement.CanClose);

This keeps the model sync but leaves the local-value slot free, so a style setter applied later
still wins and still propagates back to the layout model through OnCanCloseChanged.

IsSelected, IsActive and LayoutAnchorableItem's Visibility sit on adjacent lines with the
same latent problem but are deliberately left as plain assignments: those are commonly bound
Mode=TwoWay in container styles (see source/MVVMTestApp/MainWindow.xaml), and SetCurrentValue
does not write back through a two-way binding, so converting them would quietly break model
write-back. Happy to revisit that separately if you'd prefer them changed too.

Tests

LayoutItemContainerStyle / LayoutItemContainerStyleSelector had no test coverage at all.

claude and others added 2 commits August 8, 2026 15:37
… over the model value

SetDefaultBindings seeded CanClose from the layout model with a plain assignment,
which stamps a local value onto the LayoutItem. A local value outranks a Style
setter, so <Setter Property="CanClose" Value="False"/> was silently discarded
whenever the container style arrived after the item already existed.

That is exactly what happens when the DataContext is declared in XAML: the
DocumentsSource binding resolves while the window is parsed, so the documents -
and their LayoutItems - are created before the parser reaches
LayoutItemContainerStyle(Selector). Assigning the DataContext in code behind
produces the opposite order, which is why the setter appeared to work there.

Seeding the value with SetCurrentValue keeps the model sync while leaving the
local-value slot free, so a style setter applied later still wins and still
propagates back to the layout model. IsSelected, IsActive and the anchorable
Visibility are deliberately left as plain assignments: those are commonly bound
Mode=TwoWay in container styles and SetCurrentValue does not write back through
a two-way binding.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019x4VAXcunCRWcKZCcumfFZ
@sharpSteff
sharpSteff merged commit 9527f31 into Dirkster99:master Aug 8, 2026
1 check passed
@sharpSteff
sharpSteff deleted the fix/505-canclose-style-setter branch August 8, 2026 16:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CanClose setter of LayoutItem Style will not work if DataContext is set in xaml

2 participants