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());
}
}
}
}