Skip to content

Commit a9b9783

Browse files
committed
Add MarkupOptions.AllowResourceBinding
Some controls now need to allow resource bindings, but not hardcoded values (i.e. grid columns)
1 parent ab70d58 commit a9b9783

File tree

10 files changed

+27
-8
lines changed

10 files changed

+27
-8
lines changed

src/Framework/Framework/Binding/DotvvmCapabilityProperty.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,13 +336,21 @@ internal static IControlAttributeDescriptor InitializeArgument(ICustomAttributeP
336336
{
337337
dotvvmProperty = new DotvvmProperty(propertyName, type, declaringType, boxedDefaultValue, false, attributeProvider);
338338
dotvvmProperty.OwningCapability = declaringCapability;
339-
339+
340340
var isNullable = propertyType.IsNullable() || type.IsNullable();
341341
if (!defaultValue.HasValue && !isNullable)
342342
dotvvmProperty.MarkupOptions._required ??= true;
343343

344+
if (typeof(IValueBinding).IsAssignableFrom(propertyType))
345+
{
346+
dotvvmProperty.MarkupOptions._allowHardCodedValue ??= false;
347+
dotvvmProperty.MarkupOptions._allowResourceBinding ??= false;
348+
}
344349
if (typeof(IBinding).IsAssignableFrom(propertyType))
350+
{
345351
dotvvmProperty.MarkupOptions._allowHardCodedValue ??= false;
352+
dotvvmProperty.MarkupOptions._allowResourceBinding ??= true;
353+
}
346354
else if (!typeof(ValueOrBinding).IsAssignableFrom(propertyType.UnwrapNullableType()))
347355
dotvvmProperty.MarkupOptions._allowBinding ??= false;
348356

src/Framework/Framework/Binding/DotvvmProperty.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ public static void InitializeProperty(DotvvmProperty property, ICustomAttributeP
426426
if (property.IsBindingProperty)
427427
{
428428
property.MarkupOptions.AllowHardCodedValue = false;
429+
property.MarkupOptions.AllowResourceBinding = !typeof(IValueBinding).IsAssignableFrom(property.PropertyType);
429430
}
430431
}
431432

src/Framework/Framework/Compilation/ControlTree/ControlTreeResolverBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ private void ValidateAttribute(IPropertyDescriptor property, IAbstractControl co
450450
return;
451451
}
452452

453-
if (!property.MarkupOptions.AllowHardCodedValue &&
453+
if (!property.MarkupOptions.AllowResourceBinding &&
454454
attribute.ValueNode is DothtmlValueBindingNode resourceBinding &&
455455
treatBindingAsHardCodedValue.Contains(resourceBinding.BindingNode.Name))
456456
{

src/Framework/Framework/Controls/CheckBox.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class CheckBox : CheckableControlBase
2222
/// <summary>
2323
/// Gets or sets whether the control is checked.
2424
/// </summary>
25-
[MarkupOptions(AllowHardCodedValue = false)]
25+
[MarkupOptions(AllowHardCodedValue = false, AllowResourceBinding = true)]
2626
public bool? Checked
2727
{
2828
get { return (bool?)GetValue(CheckedProperty); }

src/Framework/Framework/Controls/DotvvmBindableObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public bool IsPropertySet(DotvvmProperty property, bool inherit = true)
6969
/// </summary>
7070
[BindingCompilationRequirements(
7171
optional: new[] { typeof(Binding.Properties.SimplePathExpressionBindingProperty) })]
72-
[MarkupOptions(AllowHardCodedValue = false)]
72+
[MarkupOptions(AllowHardCodedValue = false, AllowResourceBinding = true)]
7373
public object? DataContext
7474
{
7575
get {

src/Framework/Framework/Controls/GridViewCheckBoxColumn.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class GridViewCheckBoxColumn : GridViewColumn
1414
/// <summary>
1515
/// Gets or sets a binding which retrieves the value to display from the current data item.
1616
/// </summary>
17-
[MarkupOptions(AllowHardCodedValue = false, Required = true)]
17+
[MarkupOptions(AllowHardCodedValue = false, AllowResourceBinding = true, Required = true)]
1818
public IStaticValueBinding ValueBinding
1919
{
2020
get { return (IStaticValueBinding)GetValueRaw(ValueBindingProperty)!; }

src/Framework/Framework/Controls/ItemsControl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public abstract class ItemsControl : HtmlGenericControl
2727
/// <summary>
2828
/// Gets or sets the source collection or a GridViewDataSet that contains data in the control.
2929
/// </summary>
30-
[MarkupOptions(AllowHardCodedValue = false)]
30+
[MarkupOptions(AllowHardCodedValue = false, AllowResourceBinding = true)]
3131
[BindingCompilationRequirements(
3232
required: new[] { typeof(DataSourceAccessBinding) },
3333
optional: new[] { typeof(DataSourceLengthBinding), typeof(CollectionElementDataContextBindingProperty) })]

src/Framework/Framework/Controls/MarkupOptionsAttribute.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ public bool AllowHardCodedValue
3333
}
3434
internal bool? _allowHardCodedValue = null;
3535

36+
/// <summary>
37+
/// Gets or sets whether the `resource` binding can be used on this property. By default, the <see cref="AllowHardCodedValue" /> is copied.
38+
/// </summary>
39+
public bool AllowResourceBinding
40+
{
41+
get => _allowResourceBinding ?? AllowHardCodedValue;
42+
set => _allowResourceBinding = value;
43+
}
44+
internal bool? _allowResourceBinding = null;
45+
3646
/// <summary>
3747
/// Gets or sets the name in markup. Null means that the name of the property should be used.
3848
/// </summary>

src/Framework/Framework/Controls/Validation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class Validation
2424
/// Note that data annotations of the property used in the target binding are not validated, only the rules inside its value.
2525
/// </summary>
2626
[AttachedProperty(typeof(object))]
27-
[MarkupOptions(AllowHardCodedValue = false)]
27+
[MarkupOptions(AllowHardCodedValue = false, AllowResourceBinding = true)]
2828
public static DotvvmProperty TargetProperty = DotvvmProperty.Register<object?, Validation>(() => TargetProperty, null, true);
2929

3030

src/Tests/ControlTests/MarkupControlTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ @wrapperTag div
144144
StringAssert.Contains(exception.Message, "cannot be used in resource-binding only data context, because it uses value bindings on the data context.");
145145

146146
// is dothtml (not dotcontrol), otherwise it's impossible to find where is it being a problem
147-
StringAssert.EndsWith("MarkupControl_ValueInResource_Error.dothtml", exception.FileName);
147+
StringAssert.EndsWith(exception.FileName, "MarkupControl_ValueInResource_Error.dothtml");
148148
}
149149

150150
[TestMethod]

0 commit comments

Comments
 (0)