question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AttachedProperty cannot be assigned in a XAML file if it is declared in the same project.

See original GitHub issue
  • .NET Core Version: 5.0.203
  • Windows version: 10.0.19043.985
  • Does the bug reproduce also in WPF for .NET Framework 4.8?: Yes

Problem description:

Declare a class with an AttachedProperty:

public static class DependencyTest
{
    public static readonly System.Windows.DependencyProperty FooProperty = System.Windows.DependencyProperty.RegisterAttached(
        "Foo", typeof(bool), typeof(DependencyTest),
        new System.Windows.PropertyMetadata(default(bool)));

    public static bool GetFoo(System.Windows.DependencyObject element) => (bool)element.GetValue(FooProperty);

    public static void SetFoo(System.Windows.DependencyObject element, bool value) => element.SetValue(FooProperty, value);
}

Use the AttachedProperty in a XAML in the same project:

<Window x:Class="Walterlv.Demo.XamlProperties.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Walterlv.Demo.XamlProperties"
        xmlns:system="clr-namespace:System;assembly=System.Runtime">
    <local:DependencyTest.Foo>
        <system:Boolean>True</system:Boolean>
    </local:DependencyTest.Foo>
    <Grid>
    </Grid>
</Window>

Then the build will fail.

If the AttachedProperty is written in another project, the build succeeds.

Actual behavior:

The error message is:

error MC3089: The object 'Window' already has a child and cannot add ''. 'Window' can accept only one child.

Expected behavior:

There is no building error and it can be treated as an AttachedProperty.

Minimal repro:

See the repo: https://github.com/walterlv/Walterlv.Issues.XamlProperties

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:10
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
Kuldeep-MScommented, Feb 21, 2023

We can not find the Assembly in NamespaceMapEntry class when the AttachedProperty is written in the same project. And we can not find the know type in CompileBamlTag in XamlReaderHelper.cs

I have added a fix here. Can you please give it a try and let me know if it works.

3reactions
walterlvcommented, May 20, 2021

The AttachedProperty can be completely distinguished from nested classes because the nested class is not supported in a XAML.

See the official docs here:

Your custom class must not be a nested class. Nested classes and the “dot” in their general CLR usage syntax interfere with other WPF and/or XAML features such as attached properties.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Visual Studio cannot find custom Attached Property in ...
The error appears with the same property across different files and as you can see once more further down in the same file....
Read more >
XAML Magic: Attached Properties
The property itself is defined as a static, read-only member of the class, which registers itself with the Dependency Property system by ...
Read more >
Attached properties overview - WPF .NET
An attached property is a Extensible Application Markup Language (XAML) concept. Attached properties enable extra property/value pairs to be set ...
Read more >
XAML overview - WPF .NET
XAML is a declarative markup language. As applied to the .NET programming model, XAML simplifies creating a UI for a .NET app.
Read more >
Msbuild get directory name. props imported, or something ...
Targets build file. You can set properties on the command line (and in response files), in special files like Directory. NET Core projects),...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found