dotnet / dotnet/wpf

System.Windows.Data.BindingExpressionBase.JoinBindingGroup throws exception

Open
#1,321 0 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

In our code we invoke bg.BindingExpressions.Add(expression)

('bg' is of type System.Windows.Data.BindingGroup, and 'expression' is of type BindingExpressionBase or a subclass)

We are finding that this sometimes throws System.InvalidOperationException "The binding expression already belongs to a BindingGroup; ..."

even though the expression's BindingGroup property is null (we have not added it to this or another BindingGroup already).

In following BindingGroup's code (.net framework version 4.0.0.0) through .NET Reflector, I observe the following:

Its Initialize method sets up the BindingExpression's collection to invoke the OnBindingsChanged method when items are added, so this method gets invoked when we call .Add above.

OnBindingsChanged, when adding an item, invokes JoinBindingGroup.

JoinBindingGroup has the following code snippet which looks fragile:
if (!item.HasValue(Feature.BindingGroup))
{
// snipped
}
else if (item.BindingGroup != bg)
{
throw new InvalidOperationException...
}

Here, item.BindingGroup is null, so that doesn't equal bg (which is not null). Why does it "think" that this means it is already added to another BindingGroup? Shouldn't this be:
else if (item.BindingGroup != null && item.BindingGroup != bg) ...?

Why does item.HasValue(Feature.BindingGroup) evaluate to true, so that it gets to that 'else' statement, when it has not yet been added to a BindingGroup?

In looking for what sets Feature.BindingGroup, it appears this is done through the internal SetValue method. SetValue(Feature.BindingGroup, ...) is called in these methods within BindingExpressionBase:
- JoinBindingGroup - when actually joining it, so that seems fine
- RejoinBindingGroup - that seems ok
- MarkAsNonGrouped - why?

MarkAsNonGrouped is called by FindBindingGroup.
Why does the attempt to 'find' a binding group apparently mark it is 'having' a binding group?
I suspect the internal behavior of this class, or perhaps of something else invoking these methods (not by our code doing this directly) is making JoinBindingGroup fail.

To work around this I am having to wrap our call to bg.BindingExpressions.Add(expression) in a try/catch block and swallow the exception. This is not a good solution obviously.

I suspect you will want me to provide source code that leads to this error. However that is not feasible. I believe you need to just do some static code analysis of the BindingExpressionBase class and make it less fragile. In this case we have truly not added the expression to a BindingGroup, yet the JoinBindingGroup method 'thinks' we have and throws an exception because of it.

_This issue has been moved from https://developercommunity.visualstudio.com/content/problem/430361/systemwindowsdatabindingexpressionbasejoinbindingg.html
VSTS ticketId: 832961_
_These are the original issue comments:_

Jack Warner on 4/3/2019, 01:22 PM (106 days ago):

Is this being looked at?

And this is odd. I created this thread but it shows that "$$ANON_USER$$" created it.

And, my posting history is gone. It thinks this is only my 3rd post and that I joined very recently.

_These are the original issue solutions:_
(no solutions)

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.