Skip to content

PriorityBinding.InitializeClone throws ArgumentOutOfRangeException #11419

@smolchanovsky

Description

@smolchanovsky

Description

PriorityBinding.InitializeClone has the same off-by-one bug that was fixed for MultiBinding in #3220 / #3221.

The fix for MultiBinding changed i<=_bindingCollection.Count to i<_bindingCollection.Count, but the identical bug in PriorityBinding was not addressed.

https://github.com/dotnet/wpf/blob/main/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/PriorityBinding.cs#L116

Reproduction Steps

var priorityBinding = new PriorityBinding();
priorityBinding.Bindings.Add(new Binding("Path1"));
priorityBinding.Bindings.Add(new Binding("Path2"));

var cloneMethod = typeof(BindingBase).GetMethod("Clone", BindingFlags.Instance | BindingFlags.NonPublic);
var clone = cloneMethod.Invoke(priorityBinding, new object[] { BindingMode.Default }); // throws ArgumentOutOfRangeException

Expected behavior

PriorityBinding should be cloned without exception

Actual behavior

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
   at System.Collections.Generic.List`1.get_Item(Int32 index)
   at System.Windows.Data.PriorityBinding.InitializeClone(BindingBase baseClone, BindingMode mode)
   at System.Windows.Data.BindingBase.Clone(BindingMode mode)

Regression?

No. This bug has existed since the code was written. The same bug in MultiBinding was fixed in #3221, but PriorityBinding was not included in that fix

Known Workarounds

Clone PriorityBinding manually by creating a new instance and copying properties/bindings

Impact

No response

Configuration

  • .NET 10.0 (also reproduces on .NET Framework 4.8)
  • Windows 11
  • x64
  • Not specific to configuration

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions