dotnet / dotnet/wpf

CommandBindingCollection.Insert doesn't work if the collection is empty

Open
#7,898 2 comments 1 reaction 0 assignees View on GitHub
Investigate
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

### Description

`CommandBindingCollection.Insert` is described here: https://learn.microsoft.com/en-us/dotnet/api/system.windows.input.commandbindingcollection.insert?view=windowsdesktop-8.0#system-windows-input-commandbindingcollection-insert(system-int32-system-windows-input-commandbinding)

Internally, `CommandBindingCollection` lazily allocates the underlying storage. The storage is created in a call to `Add`: https://github.com/dotnet/wpf/blob/main/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/CommandBindingCollection.cs#L172-L185

However, `Insert` does not create the underlying storage if it doesn't exist:
https://github.com/dotnet/wpf/blob/main/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/CommandBindingCollection.cs#L220-L230

### Reproduction Steps

```
var collection = new CommandBindingCollection();
collection.Insert(0, new CommandBinding());
Assert.Equal(1, collection.Count);
```

### Expected behavior

The test passes - the item is inserted at index 0.

### Actual behavior

The test fails - the item is not inserted at index 0, and the count is 0.

### Regression?

_No response_

### Known Workarounds

_No response_

### Impact

_No response_

### Configuration

_No response_

### Other information

_No response_

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.