Binding on two different attached properties with the same local name causes both properties to be resolved to one of them
- 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`

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

Contributor guide
Assessment
This issue has not been assessed yet.