在 ListboxItem 上添加自定义混合行为 [英] Adding custom blend behaviour on ListboxItem

查看:19
本文介绍了在 ListboxItem 上添加自定义混合行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建自定义混合行为以作用于 ListboxItem 即

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?

或者,我是否必须设置 Listbox 项目的样式并将其附加到 Border 或样式中的任何其他元素?如果这是真的,那么我是否还必须从 Border 派生我的行为类(即框架元素)?

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>

推荐答案

我基本上已经完成了eran otzap 在他的评论中提出的建议,以及这篇文章,作者是 Mark Smith.这允许仍然使用混合行为及其优点(如 AssociatedObject),但在附加它时更加灵活.它融合了正常的附加行为和混合行为.

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.

ItemContainerStyle中附加行为:

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

行为

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

这篇关于在 ListboxItem 上添加自定义混合行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆