2009-05-29 7 views
0

J'ai mis en œuvre le attached command behavior pattern found here et fonctionne bien pour permettre par exemple. une bordure d'avoir un événement ou gauche clic droit que les incendies dans le ViewModel:Comment attacher deux comportements attachés à un élément XAML?

XAML:

<Border Background="Yellow" Width="350" Margin="0,0,10,0" Height="35" CornerRadius="2" 
     c:CommandBehavior.Event="MouseLeftButtonDown" 
     c:CommandBehavior.Command="{Binding PressedLeftButton}" 
     c:CommandBehavior.CommandParameter="MainBorder123"> 
    <TextBlock Text="this is the click area"/> 
</Border> 

code Derrière:

public ICommand PressedLeftButton { get; private set; } 

public MainViewModel() 
{ 

    Output = "original value"; 

    PressedLeftButton = new SimpleCommand 
    { 
     ExecuteDelegate = parameterValue => { 
      Output = String.Format("left mouse button was pressed at {0} and sent the parameter value \"{1}\"", DateTime.Now.ToString(), parameterValue.ToString()); 
     } 
    }; 
} 

Cependant, comment faire J'attache deux comportements attachés à un élément, par exemple Je veux faire quelque chose comme ce qui suit, mais bien sûr, il me donne une erreur:

<Border Background="Yellow" Width="350" Margin="0,0,10,0" Height="35" CornerRadius="2" 
     c:CommandBehavior.Event="MouseLeftButtonDown" 
     c:CommandBehavior.Command="{Binding PressedLeftButton}" 
     c:CommandBehavior.CommandParameter="MainBorder123" 
     c:CommandBehavior.Event="MouseRightButtonDown" 
     c:CommandBehavior.Command="{Binding PressedRighttButton}" 
     c:CommandBehavior.CommandParameter="MainBorder123" 
     > 

Répondre

5

Le lien que vous avez envoyé contient cette réponse très. Vous pouvez utiliser les capacités CommandBehaviorCollection.Behaviors dans ACB v2.

<Border Background="Yellow" Width="350" Margin="0,0,10,0" Height="35" CornerRadius="2" x:Name="test"> 
     <local:CommandBehaviorCollection.Behaviors> 
       <local:BehaviorBinding Event="MouseLeftButtonDown" Action="{Binding DoSomething}" CommandParameter="An Action on MouseLeftButtonDown"/> 
       <local:BehaviorBinding Event="MouseRightButtonDown" Command="{Binding SomeCommand}" CommandParameter="A Command on MouseRightButtonDown"/> 
     </local:CommandBehaviorCollection.Behaviors> 
     <TextBlock Text="MouseDown on this border to execute the command"/> 
    </Border> 
+0

c'était tout, merci, mais drôle que mon éditeur XAML me donne l'erreur "La propriété attachable 'Behaviors' n'a pas été trouvé dans le type 'CommandBehaviorCollection'." Bien que je puisse courir et compiler correctement, pourquoi? –

+5

Je ne sais pas, mais je n'ai jamais fait confiance aux éditeurs XAML. :) –

0

« qui était, merci, drôle bien que mon éditeur XAML me donne l'erreur « La propriété attachable « Behaviors » n'a pas été trouvé dans le type « CommandBehaviorCollection ». » Bien que je peux courir et compiler bien, pourquoi donc?"

La raison est que le code qui permet la collection de comportements de commande (qui est une propriété jointe collection) est actuellement un type de faille XAML. Vous pouvez lire plus à ce sujet ici: http://wekempf.spaces.live.com/blog/cns!D18C3EC06EA971CF!468.entry?sa=276442122

Questions connexes