satorp
Topic Author
Posts: 20
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?
 
User avatar
sfernandez
Site Admin
Posts: 2983
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: 20
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: 193
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: 2983
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
 
christyjquinn
Posts: 17
Joined: 17 Aug 2022, 17:03

Re: Assertion when using TemplateBinding

22 Oct 2022, 02:04

Sorry to open back up quite an old thread but I am encountering this issue at the moment. The problem is I'm having a hard time identifying the exact TemplateBinding that is causing the issue. Any tips to debug this further?
 
User avatar
sfernandez
Site Admin
Posts: 2983
Joined: 22 Dec 2011, 19:20

Re: Assertion when using TemplateBinding

24 Oct 2022, 10:45

Hi, could you please elaborate a bit more, because the TemplateBinding code does not assert anymore, it uses the Converter if it exists, or returns the source value otherwise.
 
christyjquinn
Posts: 17
Joined: 17 Aug 2022, 17:03

Re: Assertion when using TemplateBinding

24 Oct 2022, 19:16

Ahh interesting, I think we must be still using an older version of Noesis that doesn't have this change. But to elaborate a bit more, I'm essentially having trouble identifying the problematic TempalteBinding when the assert throws.
 
christyjquinn
Posts: 17
Joined: 17 Aug 2022, 17:03

Re: Assertion when using TemplateBinding

25 Oct 2022, 19:55

Just a quick update, I've identified the problematic TemplateBinding and replaced it with the above fix. So all good there!
 
User avatar
sfernandez
Site Admin
Posts: 2983
Joined: 22 Dec 2011, 19:20

Re: Assertion when using TemplateBinding

26 Oct 2022, 12:10

Thanks for the update. I guess if you got an assert you have debugged it and checked the TemplateBinding expression data: the templated parent, and the binding target object and property. With that information it should be easy to identify the problematic binding.

Anyway, we still have to solve a problem with bindings and converters: #1376. We will fix it for our next major version.

Who is online

Users browsing this forum: Google [Bot] and 13 guests