Xaml uwp定义eventriggerbehavior内部样式

Xaml uwp定义eventriggerbehavior内部样式,xaml,uwp,Xaml,Uwp,我现在遇到了一个问题。我想为我的所有ListView声明eventriggerbehavior。这是我的代码: <Style TargetType="ListView"> <Setter Property="ItemTemplate" Value="{StaticResource itemShowTemplate}" /> <i:Interaction.Behaviors> <

我现在遇到了一个问题。我想为我的所有ListView声明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正在查找样式上的事件,而不是Listview的targettype。在EventTriggerBehavior上唯一要设置的属性是SourceObject。但我不希望在1个listview上出现这种行为,我希望在我的所有listview上都出现这种行为


有办法做到这一点吗?

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

     <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>
然后在样式中按如下方式附加:

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>


这很有效。谢谢你的帮助,这个问题的一个很好的例子已经在这里有了答案: