添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Support
Version: 11.0.x

How To Create Attached Properties

When you need more or let's say foreign properties on avalonia elements, then attached properties are the right thing to use. They can also be used to create so called behaviors to generally modify the hosted gui components. This can be utilized to bind a command to an event for instance.

Here we present an example of how to use a command in an MVVM compatible way and bind it to an event.

It may not be the ideal solution as there are projects such as Avalonia Behaviors where this is properly done. But it illustrates the following two learnings:

  • How to create attached properties in Avalonia UI
  • How to use them in a MVVM way.
  • First we have to create our attached property. The method AvaloniaProperty.RegisterAttached is used for that purpose. Note that by convention the public static CLR-property for the attached property is named XxxxProperty . Also note that by convention the name (the parameter) of the attached property is Xxxx without the Property . And finally note that by convention one must provide two public static methods called SetXxxx(element,value) and GetXxxx(element) .

    This call ensures that the property has a type, an owner type and one where it may be used.

    The verify method can be used to clean up a value that is being set. Either by returning the corrected value or discard the process by returning AvaloniaProperty.UnsetValue . Or one can perform special tasks with the element that the property is hosted by. The getter and setter methods should always just set the value and never do anything beyond. In fact they will usually never be called as the Binding system will recognize the convention and set the properties directly where they are stored.

    In this example file we create two attached properties that interact with each other: A Command property and a CommandParameter that is used by when invoking the command.

    /// <summary>
    /// Container class for attached properties. Must inherit from <see cref="AvaloniaObject"/>.
    /// </summary>
    public class DoubleTappedBehav : AvaloniaObject
    {
    static DoubleTappedBehav()
    {
    CommandProperty.Changed.AddClassHandler<Interactive>(HandleCommandChanged);
    }

    /// <summary>
    /// Identifies the <seealso cref="CommandProperty"/> avalonia attached property.
    /// </summary>
    /// <value>Provide an <see cref="ICommand"/> derived object or binding.</value>
    public static readonly AttachedProperty<ICommand> CommandProperty = AvaloniaProperty.RegisterAttached<DoubleTappedBehav, Interactive, ICommand>(
    "Command", default(ICommand), false, BindingMode.OneTime);

    /// <summary>
    /// Identifies the <seealso cref="CommandParameterProperty"/> avalonia attached property.
    /// Use this as the parameter for the <see cref="CommandProperty"/>.
    /// </summary>
    /// <value>Any value of type <see cref="object"/>.</value>
    public static readonly AttachedProperty<object> CommandParameterProperty = AvaloniaProperty.RegisterAttached<DoubleTappedBehav, Interactive, object>(
    "CommandParameter", default(object), false, BindingMode.OneWay, null);


    /// <summary>
    /// <see cref="CommandProperty"/> changed event handler.
    /// </summary>
    private static void HandleCommandChanged(Interactive interactElem, AvaloniaPropertyChangedEventArgs args)
    {
    if (args.NewValue is ICommand commandValue)
    {
    // Add non-null value
    interactElem. AddHandler(InputElement.DoubleTappedEvent, Handler);
    }
    else
    {
    // remove prev value
    interactElem.RemoveHandler(InputElement.DoubleTappedEvent, Handler);
    }
    // local handler fcn
    static void Handler(object s, RoutedEventArgs e)
    {
    if (s is Interactive interactElem)
    {
    // This is how we get the parameter off of the gui element.
    object commandParameter = interactElem.GetValue(CommandParameterProperty);
    ICommand commandValue = interactElem.GetValue(CommandProperty);
    if (commandValue?.CanExecute(commandParameter) == true)
    {
    commandValue.Execute(commandParameter);
    }
    }
    }
    }


    /// <summary>
    /// Accessor for Attached property <see cref="CommandProperty"/>.
    /// </summary>
    public static void SetCommand(AvaloniaObject element, ICommand commandValue)
    {
    element.SetValue(CommandProperty, commandValue);
    }

    /// <summary>
    /// Accessor for Attached property <see cref="CommandProperty"/>.
    /// </summary>
    public static ICommand GetCommand(AvaloniaObject element)
    {
    return element.GetValue(CommandProperty);
    }

    /// <summary>
    /// Accessor for Attached property <see cref="CommandParameterProperty"/>.
    /// </summary>
    public static void SetCommandParameter(AvaloniaObject element, object parameter)
    {
    element.SetValue(CommandParameterProperty, parameter);
    }

    /// <summary>
    /// Accessor for Attached property <see cref="CommandParameterProperty"/>.
    /// </summary>
    public static object GetCommandParameter(AvaloniaObject element)
    {
    return element.GetValue(CommandParameterProperty);
    }
    }

    In the verify method we utilize the routed event system to attach a new handler. Note that the handler should be detached, again. The value of the property is requested by the normal program mechanisms using GetValue() method.