WPF: can I define / set an attached property via style?

Is there a way to set an attached property through a style?

I have, for example, a button on which the interaction material is installed (from System.Windows.Interactivity)

<Button>
  <i:Interaction.Triggers>
    ...
  </i:Interaction.Triggers>
</Button>

Now I would like to create a style in which this Interaction.Triggers property is set, thereby replacing redundancy, indicating this property on each Button instance. Is this possible in WPF?

<Style Target={x:Type Button}>
  <!-- ??? -->
  <Setter PropertyName="i.Interaction.Triggers">
  ...

Somehow I canโ€™t understand how, but I saw other examples on the Internet where the attached properties seem to be accessible from within the style ...

Update

So basically this is more of a problem with Interaction.Triggers, which has no way to โ€œinstallโ€ something. How can I reuse a set of interaction definitions?

+5
1

( InputBindings). , :

public static StyleTriggerCollection GetTriggers(DependencyObject obj) {
    return (StyleTriggerCollection)obj.GetValue(TriggersProperty);
}

public static void SetTriggers(DependencyObject obj, StyleTriggerCollection value) {
    obj.SetValue(TriggersProperty, value);
}

public static readonly DependencyProperty TriggersProperty =
    DependencyProperty.RegisterAttached("Triggers", typeof(StyleTriggerCollection), typeof(ControlExtensions), new UIPropertyMetadata(null, OnTriggersChanged));

static void OnTriggersChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
    var triggers = (StyleTriggerCollection) e.NewValue;

    if (triggers != null) {
        var existingTriggers = Interaction.GetTriggers(d);

        foreach (var trigger in triggers) {
            existingTriggers.Add((TriggerBase)trigger.Clone());
        }
    }
}

StyleTriggerCollection, :

public class StyleTriggerCollection : Collection<TriggerBase>
{
}

:

<Setter Property="my:ControlExtensions.Triggers">
    <Setter.Value>
        <my:StyleTriggerCollection>
            <!-- Put your triggers here -->
        </my:StyleTriggerCollection>
    </Setter.Value>
</Setter>
+8

All Articles