Skip to content

Commit 1d82ab0

Browse files
authored
Revert "[X] do not apply Bindings if DataType doesnt match (#22056)" (#24046)
This reverts commit cb0a332.
1 parent 1aac6a6 commit 1d82ab0

File tree

5 files changed

+8
-93
lines changed

5 files changed

+8
-93
lines changed

src/Controls/src/Core/Binding.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.ComponentModel;
55
using System.Globalization;
66
using System.Reflection;
7-
using Microsoft.Maui.Controls.Xaml.Diagnostics;
87

98
namespace Microsoft.Maui.Controls
109
{
@@ -106,8 +105,6 @@ public string UpdateSourceEventName
106105
}
107106
}
108107

109-
internal Type DataType { get; set; }
110-
111108
internal override void Apply(bool fromTarget)
112109
{
113110
base.Apply(fromTarget);
@@ -123,14 +120,7 @@ internal override void Apply(object context, BindableObject bindObj, BindablePro
123120
object src = _source;
124121
var isApplied = IsApplied;
125122

126-
var bindingContext = src ?? Context ?? context;
127-
if (DataType != null && bindingContext != null && !DataType.IsAssignableFrom(bindingContext.GetType()))
128-
{
129-
BindingDiagnostics.SendBindingFailure(this, "Binding", "Mismatch between the specified x:DataType and the current binding context");
130-
bindingContext = null;
131-
}
132-
133-
base.Apply(bindingContext, bindObj, targetProperty, fromBindingContextChanged, specificity);
123+
base.Apply(src ?? context, bindObj, targetProperty, fromBindingContextChanged, specificity);
134124

135125
if (src != null && isApplied && fromBindingContextChanged)
136126
return;
@@ -141,6 +131,7 @@ internal override void Apply(object context, BindableObject bindObj, BindablePro
141131
}
142132
else
143133
{
134+
object bindingContext = src ?? Context ?? context;
144135
if (_expression == null)
145136
_expression = new BindingExpression(this, SelfPath);
146137
_expression.Apply(bindingContext, bindObj, targetProperty, specificity);

src/Controls/src/Core/IXamlDataTypeProvider.cs

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/Controls/src/Xaml/MarkupExtensions/BindingExtension.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using System;
22
using System.ComponentModel;
3-
using System.Linq.Expressions;
43
using Microsoft.Maui.Controls.Internals;
54

65
namespace Microsoft.Maui.Controls.Xaml
76
{
87
[ContentProperty(nameof(Path))]
8+
[AcceptEmptyServiceProvider]
99
public sealed class BindingExtension : IMarkupExtension<BindingBase>
1010
{
1111
public string Path { get; set; } = Binding.SelfPath;
@@ -21,22 +21,13 @@ public sealed class BindingExtension : IMarkupExtension<BindingBase>
2121

2222
BindingBase IMarkupExtension<BindingBase>.ProvideValue(IServiceProvider serviceProvider)
2323
{
24-
if (TypedBinding is null) {
25-
Type bindingXDataType = null;
26-
if ((serviceProvider.GetService(typeof(IXamlTypeResolver)) is IXamlTypeResolver typeResolver)
27-
&& (serviceProvider.GetService(typeof(IXamlDataTypeProvider)) is IXamlDataTypeProvider dataTypeProvider)
28-
&& dataTypeProvider.BindingDataType != null)
29-
{
30-
typeResolver.TryResolve(dataTypeProvider.BindingDataType, out bindingXDataType);
31-
}
24+
if (TypedBinding == null)
3225
return new Binding(Path, Mode, Converter, ConverterParameter, StringFormat, Source)
3326
{
3427
UpdateSourceEventName = UpdateSourceEventName,
3528
FallbackValue = FallbackValue,
3629
TargetNullValue = TargetNullValue,
37-
DataType = bindingXDataType,
3830
};
39-
}
4031

4132
TypedBinding.Mode = Mode;
4233
TypedBinding.Converter = Converter;

src/Controls/src/Xaml/XamlServiceProvider.cs

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ internal XamlServiceProvider(INode node, HydrationContext context)
2727
IXmlLineInfoProvider = new XmlLineInfoProvider(xmlLineInfo);
2828

2929
IValueConverterProvider = new ValueConverterProvider();
30-
31-
if (node is IElementNode elementNode)
32-
Add(typeof(IXamlDataTypeProvider), new XamlDataTypeProvider(elementNode));
3330
}
3431

3532
public XamlServiceProvider() => IValueConverterProvider = new ValueConverterProvider();
@@ -265,57 +262,4 @@ public string LookupNamespace(string prefix)
265262
public string LookupPrefix(string namespaceName) => throw new NotImplementedException();
266263
public void Add(string prefix, string ns) => namespaces.Add(prefix, ns);
267264
}
268-
269-
class XamlDataTypeProvider : IXamlDataTypeProvider
270-
{
271-
public XamlDataTypeProvider(IElementNode node)
272-
{
273-
static IElementNode GetParent(IElementNode node)
274-
{
275-
return node switch
276-
{
277-
{ Parent: ListNode { Parent: IElementNode parentNode } } => parentNode,
278-
{ Parent: IElementNode parentNode } => parentNode,
279-
_ => null,
280-
};
281-
}
282-
283-
static bool IsBindingContextBinding(IElementNode node)
284-
{
285-
if ( ApplyPropertiesVisitor.TryGetPropertyName(node, node.Parent, out XmlName name)
286-
&& name.NamespaceURI == ""
287-
&& name.LocalName == nameof(BindableObject.BindingContext))
288-
return true;
289-
return false;
290-
}
291-
292-
INode dataTypeNode = null;
293-
IElementNode n = node as IElementNode;
294-
295-
// Special handling for BindingContext={Binding ...}
296-
// The order of checks is:
297-
// - x:DataType on the binding itself
298-
// - SKIP looking for x:DataType on the parent
299-
// - continue looking for x:DataType on the parent's parent...
300-
IElementNode skipNode = null;
301-
if (IsBindingContextBinding(node))
302-
{
303-
skipNode = GetParent(node);
304-
}
305-
306-
while (n != null)
307-
{
308-
if (n != skipNode && n.Properties.TryGetValue(XmlName.xDataType, out dataTypeNode))
309-
{
310-
break;
311-
}
312-
313-
n = GetParent(n);
314-
}
315-
if (dataTypeNode is ValueNode valueNode)
316-
BindingDataType = valueNode.Value as string;
317-
318-
}
319-
public string BindingDataType { get; }
320-
}
321265
}

src/Controls/tests/Xaml.UnitTests/BindingsCompiler.xaml.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ public class Tests
2828
{
2929
[SetUp] public void Setup() => DispatcherProvider.SetCurrent(new DispatcherProviderStub());
3030
[TearDown] public void TearDown() => DispatcherProvider.SetCurrent(null);
31-
32-
[Test]
33-
public void TestCompiledBindings([Values(false, true)]bool useCompiledXaml)
31+
32+
[TestCase(false)]
33+
[TestCase(true)]
34+
public void Test(bool useCompiledXaml)
3435
{
3536
if (useCompiledXaml)
3637
MockCompiler.Compile(typeof(BindingsCompiler));
@@ -112,13 +113,6 @@ public void TestCompiledBindings([Values(false, true)]bool useCompiledXaml)
112113
layout.BindingContext = new object();
113114
Assert.AreEqual(null, layout.label0.Text);
114115
}
115-
116-
[Test]
117-
public void BindingsNotAppliedWithWrongContext([Values(false, true)]bool useCompiledXaml)
118-
{
119-
var page = new BindingsCompiler(useCompiledXaml) { BindingContext = new {Text="Foo"} };
120-
Assert.AreEqual(null, page.label0.Text);
121-
}
122116
}
123117
}
124118

0 commit comments

Comments
 (0)