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:
- Created 2 years ago
- Reactions:10
- Comments:7 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have added a fix here. Can you please give it a try and let me know if it works.
The AttachedProperty can be completely distinguished from nested classes because the nested class is not supported in a XAML.
See the official docs here: