Assertion when using TemplateBinding
Suppose we have a TemplateBinding between dependency properties A and 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:
Rewriting to an equivalent expression
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:
Code: Select all
A={TemplateBinding B}
This works on Release build, but will generate an assertion on Debug build:
Code: Select all
NS_ASSERT(mConverter != 0);
Code: Select all
A={Binding B, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}
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?
-
sfernandez
Site Admin
- Posts: 2983
- Joined:
Re: Assertion when using TemplateBinding
It seems it is not correctly implemented in Noesis, as it works fine in WPF. Could you please report it in the bugtracker?Is the behavior of requiring type identity (not compatibility) for TemplateBinding is by design? WPF does not seem to have this restriction.
It should, it is something we didn't support yet: #1611Since TemplateBinding does not support converters, why is there an assertion for converter?
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.
Re: Assertion when using TemplateBinding
@sfernandez
Thank you for the reply. I've reported this issue in #1957. The workaround is also acceptable for us in the meantime.
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:
Re: Assertion when using TemplateBinding
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.
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.
-
sfernandez
Site Admin
- Posts: 2983
- Joined:
Re: Assertion when using TemplateBinding
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
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:
Re: Assertion when using TemplateBinding
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?
-
sfernandez
Site Admin
- Posts: 2983
- Joined:
Re: Assertion when using TemplateBinding
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:
Re: Assertion when using TemplateBinding
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:
Re: Assertion when using TemplateBinding
Just a quick update, I've identified the problematic TemplateBinding and replaced it with the above fix. So all good there!
-
sfernandez
Site Admin
- Posts: 2983
- Joined:
Re: Assertion when using TemplateBinding
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.
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