using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Templates;
using Avalonia.Data;
namespace Avalonia.Controls
{
///
/// A control which displays an error notifier when there is a DataValidationError.
/// Provides attached properties to track errors on a control
///
///
/// You will probably only want to create instances inside of control templates.
///
[PseudoClasses(":error")]
public class DataValidationErrors : ContentControl
{
///
/// Defines the DataValidationErrors.Errors attached property.
///
public static readonly AttachedProperty> ErrorsProperty =
AvaloniaProperty.RegisterAttached>("Errors");
///
/// Defines the DataValidationErrors.HasErrors attached property.
///
public static readonly AttachedProperty HasErrorsProperty =
AvaloniaProperty.RegisterAttached("HasErrors");
public static readonly StyledProperty ErrorTemplateProperty =
AvaloniaProperty.Register(nameof(ErrorTemplate));
private Control _owner;
public static readonly DirectProperty OwnerProperty =
AvaloniaProperty.RegisterDirect(
nameof(Owner),
o => o.Owner,
(o, v) => o.Owner = v);
public Control Owner
{
get { return _owner; }
set { SetAndRaise(OwnerProperty, ref _owner, value); }
}
///
/// Initializes static members of the class.
///
static DataValidationErrors()
{
ErrorsProperty.Changed.Subscribe(ErrorsChanged);
HasErrorsProperty.Changed.Subscribe(HasErrorsChanged);
TemplatedParentProperty.Changed.AddClassHandler((x, e) => x.OnTemplatedParentChange(e));
}
private void OnTemplatedParentChange(AvaloniaPropertyChangedEventArgs e)
{
if (Owner == null)
{
Owner = (e.NewValue as Control);
}
}
public IDataTemplate ErrorTemplate
{
get { return GetValue(ErrorTemplateProperty); }
set { SetValue(ErrorTemplateProperty, value); }
}
private static void ErrorsChanged(AvaloniaPropertyChangedEventArgs e)
{
var control = (Control)e.Sender;
var errors = (IEnumerable