satorp
Topic Author
Posts: 13
Joined: 12 Mar 2018, 05:48

Assertion when using TemplateBinding

31 Mar 2021, 18:35

Suppose we have a TemplateBinding between dependency properties A and B
A={TemplateBinding B}
where the types of A and B are not identical, but compatible (i.e A is assignable from B).

This works on Release build, but will generate an assertion on Debug build:
NS_ASSERT(mConverter != 0);
Rewriting to an equivalent expression
A={Binding B, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}
will suppress the assertion. I suspect that since TemplateBinding is a markup extension, the type checking is done during parsing stage, and may work differently from Binding expression.

My questions are:
  • Is the behavior of requiring type identity (not compatibility) for TemplateBinding is by design? WPF does not seem to have this restriction.
  • Since TemplateBinding does not support converters, why is there an assertion for converter?

Tags:
 
User avatar
sfernandez
Site Admin
Posts: 2109
Joined: 22 Dec 2011, 19:20

Re: Assertion when using TemplateBinding

01 Apr 2021, 13:51

Is the behavior of requiring type identity (not compatibility) for TemplateBinding is by design? WPF does not seem to have this restriction.
It seems it is not correctly implemented in Noesis, as it works fine in WPF. Could you please report it in the bugtracker?
Since TemplateBinding does not support converters, why is there an assertion for converter?
It should, it is something we didn't support yet: #1611

The easy workaround is to use a Binding with RelativeSource TemplatedParent, in fact I've just found that WPF is internally doing that, converting TemplateBindings in templates to a Binding when the template is applied.
 
satorp
Topic Author
Posts: 13
Joined: 12 Mar 2018, 05:48

Re: Assertion when using TemplateBinding

01 Apr 2021, 15:21

@sfernandez
Thank you for the reply. I've reported this issue in #1957. The workaround is also acceptable for us in the meantime.
 
KeldorKatarn
Posts: 96
Joined: 30 May 2014, 10:26

Re: Assertion when using TemplateBinding

07 Apr 2021, 14:45

I just wanted to quickly add here That [TemplateBinding XZY} is not quite the same as using RelativeSource TemplatedParent.
TemplateBinding is resolved at compile time (at least in WPF), while RelativeSource is used at runtime. So there absolutely is a difference here that could explain why
one behaves differently in this case from the other.
Since it's runtime resolved it is also slower than TemplateBinding, which allows for a much faster template creation.
 
User avatar
sfernandez
Site Admin
Posts: 2109
Joined: 22 Dec 2011, 19:20

Re: Assertion when using TemplateBinding

09 Apr 2021, 10:30

I was referring to the moment that a TemplateBinding value was set to the VisualTree when applying the Template: https://referencesource.microsoft.com/# ... ate.cs,822

WPF implementation just creates a normal Binding with RelativeSource.TemplatedParent and uses that.

In fact the implementation of TemplateBindingExpression does nothing, just returns the default value of the property:
https://referencesource.microsoft.com/# ... sion.cs,29

Who is online

Users browsing this forum: No registered users and 4 guests