Die Idee dahinter ist, Att. Prop. mit einer Zeile erzeugen zu können:
z.B.
public class SecurityIdProperty : BaseAttachedProperty<SecurityIdProperty, string> { }
Angewendet werden soll das dann z.B. so:
<TextBox local:SecurityIdProperty.Value="123" ... />
Jetzt aber kommt das, was ich nicht verstehe: Zum einen kennt XAML SecurityIdProperty nicht, statt dessen aber bietet mir die 'Intelligence' local:BaseAttachedProperty`2 an.
Vor allem das `2 lässt mich neugierig zurück ...
Das ganze ist ja nicht meine Idee, der Programmierer im Video macht es aber genau so ...
Irgendjemand ne Idee, was mir VS da sagen möchte?
public abstract class BaseAttachedProperty<Parent, Property>
where Parent : BaseAttachedProperty<Parent, Property>, new()
{
#region Public Events
/// <summary>
/// Fired when the value changes
/// </summary>
public event Action<DependencyObject, DependencyPropertyChangedEventArgs> ValueChanged = (sender, e) => { };
#endregion
#region Public Properties
/// <summary>
/// A singleton instance of our parent class
/// </summary>
public static Parent Instance { get; private set; } = new Parent();
#endregion
#region Attached Property Definitions
/// <summary>
/// The attached property for this class
/// </summary>
public static readonly DependencyProperty ValueProperty = DependencyProperty.RegisterAttached("Value", typeof(Property), typeof(BaseAttachedProperty<Parent, Property>), new UIPropertyMetadata(new PropertyChangedCallback(OnValuePropertyChanged)));
/// <summary>
/// The callback event when the <see cref="ValueProperty"/> is changed
/// </summary>
/// <param name="d">The UI element that had it's property changed</param>
/// <param name="e">The arguments for the event</param>
private static void OnValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
// Call the parent function
Instance.OnValueChanged(d, e);
// Call event listeners
Instance.ValueChanged(d, e);
}
/// <summary>
/// Gets the attached property
/// </summary>
/// <param name="d">The element to get the property from</param>
/// <returns></returns>
public static Property GetValue(DependencyObject d) => (Property)d.GetValue(ValueProperty);
/// <summary>
/// Sets the attached property
/// </summary>
/// <param name="d">The element to get the property from</param>
/// <param name="value">The value to set the property to</param>
public static void SetValue(DependencyObject d, Property value) => d.SetValue(ValueProperty, value);
#endregion
#region Event Methods
/// <summary>
/// The method that is called when any attached property of this type is changed
/// </summary>
/// <param name="sender">The UI element that this property was changed for</param>
/// <param name="e">The arguments for this event</param>
public virtual void OnValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { }
#endregion
}