XAML uwp在样式内定义事件触发器行为

7dl7o3gd  于 2023-01-15  发布在  其他
关注(0)|答案(1)|浏览(151)

我现在遇到了一个问题。我想为我所有的列表视图声明一个eventriggerbehavior。这是我的代码:

<Style TargetType="ListView">
            <Setter Property="ItemTemplate" Value="{StaticResource itemShowTemplate}" />
            <i:Interaction.Behaviors>
                <Interactions:EventTriggerBehavior EventName="ItemClicked">
                    <Interactions:InvokeCommandAction Command="{Binding ShowItemClickedCommand}" />
                </Interactions:EventTriggerBehavior>
            </i:Interaction.Behaviors>
</Style>

我现在遇到的问题是EventTriggerBehavior在Style上寻找事件,而不是Listview的targettype。并且EventTriggerBehavior上唯一需要设置的属性是SourceObject。但是我不想在一个列表视图上使用这个行为,我想在所有列表视图上使用它。
有办法做到吗?

ljsrvy3e

ljsrvy3e1#

我的第一个想法是它可以这样工作:

<Style TargetType="Button">
    <Setter Property="i:Interaction.Behaviors">
        <Setter.Value>
            <i:BehaviorCollection>
                <core:EventTriggerBehavior EventName="Click">
                    <core:InvokeCommandAction Command="{Binding TestCommand}" />
                </core:EventTriggerBehavior>
            </i:BehaviorCollection>
        </Setter.Value>
    </Setter>
</Style>

但不幸的是,这只适用于第一个附加了行为的控件,原因是值只构造了一次,并且BehaviorCollectionAssociatedObject属性被设置为第一个控件。
然后我介绍了这个解决方案-How to add a Blend Behavior in a Style Setter,其思想是创建一个附加属性,手动将行为分配给控件。
基于此,我建议你这样做:

public static class ListViewBehaviorAttacher
{
    public static readonly DependencyProperty IsAttachedProperty = DependencyProperty.RegisterAttached(
        "IsAttached", typeof(bool), typeof(ListViewBehaviorAttacher), new PropertyMetadata(default(bool), IsAttachedChanged));

    private static void IsAttachedChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
    {
        var listView = (ListView)dependencyObject;
        //create the binding
        BehaviorCollection collection = new BehaviorCollection();
        var eventTrigger = new EventTriggerBehavior() { EventName = "ItemClick" };
        var invokeCommandAction = new InvokeCommandAction();
        //binding to command
        BindingOperations.SetBinding(
            invokeCommandAction,
            InvokeCommandAction.CommandProperty,
            new Binding() { Path = new PropertyPath("ShowItemClickedCommand"), Source = listView.DataContext });
        eventTrigger.Actions.Add(invokeCommandAction);
        collection.Add(eventTrigger);
        listView.SetValue(Interaction.BehaviorsProperty, collection);
    }

    public static void SetIsAttached(DependencyObject element, bool value)
    {
        element.SetValue(IsAttachedProperty, value);
    }

    public static bool GetIsAttached(DependencyObject element)
    {
        return (bool)element.GetValue(IsAttachedProperty);
    }
}

然后在样式中这样附加:

<Style TargetType="ListView">
   <Setter Property="SelectionMode" Value="None"></Setter>
   <Setter Property="IsItemClickEnabled" Value="True" /> 
   <Setter Property="local:ListViewBehaviorAttacher.IsAttached" Value="True" />
</Style>

相关问题