2016-08-20 3 views
1

私は現在問題が残っています。私はすべての私のリストビューに対してイベントトリガービヘイビアを宣言したいと思います。これは私のコードです:uwpはスタイル内でイベントリガーの挙動を定義します

<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スタイルではなくリストビューのTARGETTYPE上のイベントを探しているということです。また、EventTriggerBehaviorで設定できるプロパティはSourceObjectだけです。しかし、私は1つのリストビューでこの動作を望んでいない、私は私のリストビューでそれをしたい。

これを行う方法はありますか?

+0

可能性のある重複したスタイルセッター内部で正常に動作していない私はあなたがこのようにそれを行う示唆これに基づき

](https://stackoverflow.com/questions/27715798/behaviors-are-not-working-properly-inside-style-setter) – Artemious

+0

この質問にはすでに回答があります:https://stackoverflow.com/a/29808226/1830814 – Artemious

答えて

1

私の最初のアイデアは、それがこのように仕事ができるということであった:

 <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> 

しかし残念ながら、これは添付の動作を取得する第一の制御のためにのみ機能します。その理由は、値が1回だけ作成され、のAssociatedObjectプロパティが最初のコントロールに設定されているためです。

私はこの解決策を引き継いだ。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> 
+0

これが動作します。助けと偉大な例をありがとう –

+0

あなたは歓迎です:-)! –

関連する問題