dotnet / dotnet/wpf

Binding on two different attached properties with the same local name causes both properties to be resolved to one of them

Open
#3,883 3 comments 0 reactions 0 assignees View on GitHub
.NET Framework Bug
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

* .NET Core Version: Not tested on core just .NET Framework, code looks the same though
* Windows version: Windows 10 Version 1709 16299.1087
* Does the bug reproduce also in WPF for .NET Framework 4.8?: Yes

**Problem description:** Given two attached properties with the same name in a class with the same name but in different namespaces and aliased with the same xmlns prefix in a xaml file, the `PropertyPathWorker` will end up using only the first encountered property. This is because `AccessorTable` uses the local name as the cache key even though the two properties with the same local name may be unrelated.

**Actual behavior:** In the second xaml file, the binding actually refers to the first attached property, while the property on the button refers to the correct attached property

**Expected behavior:** The correct property is used in each xaml file both on the target control and in the binding

**Minimal repro:**

*MainWindow.xaml*
```xaml




```

*UserControl1.xaml*
```xaml


1









```

*UserControl1.xaml.cs*

```cs
using System.Windows;
using System.Windows.Controls;

namespace BugRepro
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
}

namespace UserControl1Props
{
public class AttachedProps
{
public static int GetSomeProp(DependencyObject obj) => (int)obj.GetValue(SomePropProperty);
public static void SetSomeProp(DependencyObject obj, int value) => obj.SetValue(SomePropProperty, value);

// Using a DependencyProperty as the backing store for SomeProp. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SomePropProperty =
DependencyProperty.RegisterAttached("SomeProp", typeof(int), typeof(AttachedProps), new PropertyMetadata(-1));
}
}
}
```

*UserControl1.xaml*
```xaml


2









```

*UserControl2.xaml.cs*
```cs
using System.Windows;
using System.Windows.Controls;

namespace BugRepro
{
public partial class UserControl2 : UserControl
{
public UserControl2()
{
InitializeComponent();
}
}

namespace UserControl2Props
{
public class AttachedProps
{
public static int GetSomeProp(DependencyObject obj) => (int)obj.GetValue(SomePropProperty);

public static void SetSomeProp(DependencyObject obj, int value) => obj.SetValue(SomePropProperty, value);

// Using a DependencyProperty as the backing store for SomeProp. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SomePropProperty =
DependencyProperty.RegisterAttached("SomeProp", typeof(int), typeof(AttachedProps), new PropertyMetadata(-2));
}
}
}
```
The code above will display two controls, both with one static text, and one through a binding on an attached property. Usercontrol1 should display 1 1, while should display Usercontrol1 should display 2 2. In actuality Usercontrol2 displays 2 -1. This is because teh binding in Usercontrol2 will use the attached property in `UserControl1Props` which has a default of `-1`

![image](https://user-images.githubusercontent.com/8193316/101184891-e5c7d500-3659-11eb-9ca9-1b94ad9357de.png)

If you change the local alias of the namespace from `c` to `cx` in any of the user controls the code works as expected:

![image](https://user-images.githubusercontent.com/8193316/101185311-625ab380-365a-11eb-86e7-943e532aa0a2.png)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.