using CommunityToolkit.Mvvm.ComponentModel;
|
using OpenTap;
|
using System.ComponentModel;
|
using System.Windows;
|
using System.Windows.Controls;
|
|
namespace OpenTapEditor
|
{
|
/// <summary>
|
/// ErrorSpan.xaml 的交互逻辑
|
/// </summary>
|
[ObservableObject]
|
public partial class ErrorSpan : UserControl
|
{
|
|
public static readonly DependencyProperty SourceProperty =
|
DependencyProperty.RegisterAttached("Source", typeof(object), typeof(ErrorSpan), new PropertyMetadata(null, OnSourceChanged));
|
|
private static void OnSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
{
|
if (d is ErrorSpan element && e.Property == SourceProperty)
|
{
|
element.Source = e.NewValue;
|
}
|
}
|
|
public object Source
|
{
|
get => GetValue(SourceProperty);
|
set
|
{
|
var old = GetValue(SourceProperty);
|
if (old is INotifyPropertyChanged ildNpc)
|
{
|
ildNpc.PropertyChanged -= Npc_PropertyChanged;
|
}
|
|
SetValue(SourceProperty, value);
|
Refresh();
|
if (value is INotifyPropertyChanged npc)
|
{
|
npc.PropertyChanged += Npc_PropertyChanged;
|
}
|
}
|
}
|
|
private void Refresh()
|
{
|
OnPropertyChanged(nameof(HasError));
|
OnPropertyChanged(nameof(ErrorMessage));
|
}
|
|
public bool HasError => SourceHasError();
|
public string ErrorMessage => SourceErrorMessage();
|
|
private void Npc_PropertyChanged(object? sender, PropertyChangedEventArgs e)
|
{
|
Refresh();
|
}
|
|
public bool SourceHasError()
|
{
|
return (Source is ValidatingObject vo) && vo?.Rules.FirstOrDefault(r => r?.IsValid() == false) != null;
|
}
|
|
public string SourceErrorMessage()
|
{
|
if (Source is ValidatingObject vo)
|
{
|
return vo?.Rules?.FirstOrDefault(r => r?.IsValid() == false)?.ErrorMessage;
|
}
|
return null;
|
}
|
|
public ErrorSpan()
|
{
|
InitializeComponent();
|
}
|
}
|
}
|