Question

I am creating a custom blend behavior to act on a ListboxItem i.e.

public class DragtestBehaviour : Behavior<ListBoxItem>
{
    public DragtestBehaviour()
    { // Insert code required on object creation below this point.
    }
    protected override void OnAttached()
    {
        base.OnAttached();
        // Insert code that you would want run when the Behavior is attached to an object.
    }
    protected override void OnDetaching()
    {
        base.OnDetaching();
        // Insert code that you would want run when the Behavior is removed from an object.
    }
}

What I am not able to figure out is how do I attach it to the listboxitem?

Alternatively, do I have to style the Listbox item and attach it to Border or any other element in style? If this is true then do I have to also derive my behavior class from Border (i.e. Framework element)?

DragtestBehaviour : Behavior<Frameworkelement>
Était-ce utile?

La solution

I have basically done what eran otzap suggested in his comment, and what is described in this article by Mark Smith. This allows to still use Blend Behaviors with its benefits (like AssociatedObject), but be more flexible about attaching it. It's a fusion of normal attached behavior with blend behavior.

For completeness sake, here's the relevant code as well.

Attach the behavior in ItemContainerStyle:

<ListBox.ItemContainerStyle>
    <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
        <Setter Property="behaviors:DragDropBehavior.IsAttached" Value="True" />
    </Style>
</ListBox.ItemContainerStyle>

Behavior

public class DragDropBehavior : Behavior<ListBoxItem>
{
    // IsAttached
    public static DependencyProperty IsAttachedProperty = DependencyProperty.RegisterAttached("IsAttached",typeof(bool), typeof(DragDropBehavior),new FrameworkPropertyMetadata(false, OnIsAttachedChanged));
    public static bool GetIsAttached(DependencyObject o){return (bool)o.GetValue(IsAttachedProperty);}
    public static void SetIsAttached(DependencyObject o, bool value){o.SetValue(IsAttachedProperty, value);}

    // is called the same as when attached in Interaction.Behaviors tag
    protected override void OnAttached()
    {            
        base.OnAttached();
    }

    // manual attachement to listbox item
    private static void OnIsAttachedChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
    {
        var el = o as UIElement;
        if (el != null)
        {
            var behColl = Interaction.GetBehaviors(el);
            var existingBehavior = behColl.FirstOrDefault(b => b.GetType() == typeof(DragDropBehavior)) as DragDropBehavior;
            if ((bool)e.NewValue == false && existingBehavior != null)
            {
                behColl.Remove(existingBehavior);
            }
            else if ((bool)e.NewValue == true && existingBehavior == null)
            {
                behColl.Add(new DragDropBehavior());
            }
        }
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top