-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
NamedElementNode.cs
49 lines (41 loc) · 1.31 KB
/
NamedElementNode.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Text;
using Avalonia.Controls;
using Avalonia.LogicalTree;
using Avalonia.Reactive;
namespace Avalonia.Data.Core.ExpressionNodes;
internal sealed class NamedElementNode : SourceNode
{
private readonly WeakReference<INameScope?> _nameScope;
private readonly string _name;
private IDisposable? _subscription;
public NamedElementNode(INameScope? nameScope, string name)
{
_nameScope = new(nameScope);
_name = name;
}
public override void BuildString(StringBuilder builder)
{
builder.Append('#');
builder.Append(_name);
}
public override bool ShouldLogErrors(object target)
{
// We don't log errors when the target element isn't rooted.
return target is not ILogical logical || logical.IsAttachedToLogicalTree;
}
protected override void OnSourceChanged(object? source, Exception? dataValidationError)
{
if (!ValidateNonNullSource(source))
return;
if (_nameScope.TryGetTarget(out var scope))
_subscription = NameScopeLocator.Track(scope, _name).Subscribe(SetValue);
else
SetError("NameScope not found.");
}
protected override void Unsubscribe(object oldSource)
{
_subscription?.Dispose();
_subscription = null;
}
}