-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simplify TemplateBindings #1695
Conversation
Rather than just use a standard `Binding`, make `TemplateBinding` a lightweight binding in the case where the binding is simply to a property on the templated parent.
Can just use TemplateBinding directly.
@@ -139,7 +139,7 @@ public object Load(Uri uri, Uri baseUri = null, object rootInstance = null) | |||
{ | |||
uriString = new Uri(baseUri, uri).AbsoluteUri; | |||
} | |||
throw new XamlLoadException("Error loading xaml at " + uriString, e); | |||
throw new XamlLoadException("Error loading xaml at " + uriString + ": " + e.Message, e); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also fixed the annoying problem where the XAML exception message didn't actually include the description of what went wrong.
@@ -72,8 +72,7 @@ | |||
<Style Selector="Slider /template/ Track#PART_Track"> | |||
<Setter Property="Minimum" Value="{TemplateBinding Minimum}"/> | |||
<Setter Property="Maximum" Value="{TemplateBinding Maximum}"/> | |||
<Setter Property="Value" Value="{TemplateBinding Path=Value, Mode=TwoWay}"/> | |||
<Setter Property="Orientation" Value="{TemplateBinding Orientation}"/> | |||
<Setter Property="Value" Value="{TemplateBinding Value, Mode=TwoWay}"/> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This setter was both unnecessary and caused problems in that it was binding to Track.Orientation
instead of Slider.Orientation
which are actually different AvaloniaProperty
s (#1696). This was resulting in the Slider
getting messed up.
public class AvaloniaPropertyTypeConverter : TypeConverter | ||
{ | ||
private static readonly Regex regex = new Regex(@"^\(?(\w*)\.(\w*)\)?|(.*)$"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This allows attached properties to be optionally specified with braces surrounding the property, as in a standard Binding
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way we can do this without using regular expressions? Regex uses lot of memory for us just checking if the string starts and ends with parentheses.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah definitely! I looked at using StringTokenizer
for this, but it wasn't suitable, and I didn't want to introduce another string tokenizer in this PR. Wasn't sure if we should make StringTokenizer
more general purpose, or just manually parse the string here, so I just used Regex
for the time being.
This shouldn't happen normally as `InpcPropertyAcessorPlugin` matches everything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As this is part of #1703 and it was tested so this PR is also tested 👍
Other than the regex, this looks good! I'll merge it in! |
Previously a
TemplateBinding
just created a normalBinding
with aRelativeSourceMode.TemplatedParent
. This PR changesTemplateBinding
to a simple binding class in its own right, with aProperty
instead of aPath
(note that this is also howTemplateBinding
works in WPF and UWP).Implementing
TemplateBinding
like this means that for the common case of binding to anAvaloniaProperty
on the template parent, no allocations need to be made other than subscribing to a couple ofPropertyChanged
events: theTemplateBinding
class itself is reused for the instantiated binding in the common case that it is bound to a single property.In WPF and UWP, two-way bindings are not supported by
TemplateBinding
. Here they are: there was no downside to allowing this and it makes it more broadly usable.Breaking Changes
TemplateBinding
s which specifyPath=
will stop working, as the property is now calledProperty
. Simply remove thePath=
qualifierTemplateBinding
s which don't simply bind to a property on the templated parent will need to be changed to regularBinding
s withRelativeSourceMode.TemplatedParent
Memory Usage
Memory usage was measured via the VS2017 diagnostic tools by running ControlCatalog in Release mode, opening all pages and then taking a memory snapshot:
Note that this PR depends on #1694:
On #1694:
This PR:
Depends on #1694