0

Often found answers here, but now it`s my first time ;)

We use the MVVM pattern in conjunction with DelegateCommands. So normally I bind a command to the button like this:

<Button Command="{Binding SetXYZActivatedCommand}" />

I need to execute different commands when the button is pressed and when the button is released again. My idea was the following:

<Button Grid.Row="3" x:Name="TestButtonObj" Content="asdlknm">
   <Button.Template>
       <ControlTemplate TargetType="{x:Type Button}">
           <Border x:Name="border" CornerRadius="80" Background="LightBlue">
              <ContentPresenter Name="content" HorizontalAlignment="Center" VerticalAlignment="Center"/>
           </Border>
           <ControlTemplate.Triggers>
              <Trigger Property="IsPressed" Value="True">
                 <Setter TargetName="border" Property="Background" Value="Aqua" />
                 <Setter TargetName="content" Property="Content" Value="Pressed" />
              </Trigger>
              <Trigger Property="ClickMode" Value="Press">
                 <Setter TargetName="TestButtonObj" Property="Command" Value="{Binding SetPttDeactivatedCommand}" />
              </Trigger>
              <Trigger Property="ClickMode" Value="Release">
                 <Setter Property="Button.Command" Value="{Binding SetPttActivatedCommand}" />
              </Trigger>
           </ControlTemplate.Triggers>
       </ControlTemplate>
   </Button.Template>
</Button>

The problem is that TestButtonObj is unknown. Ok I have accepted, that I cannot access the parent object. Without TargetName="TestButtonObj" it compiles, but the command is not executed. Mhhhh...

Ok I tried the following, but it cannot work... CommandBinding is not a dependency property (hopefully you get me out of this labyrinth)

<Button Grid.Row="2" Content="CommandBindings">
<Button.CommandBindings>
    <CommandBinding Command="{Binding SetPttActivatedCommand}" />
</Button.CommandBindings>

At this point I stuck. I don`t know if the way was completly wrong. I read the whole day docs about commands and binding, but I don't get the clue. I hope, someone can show me the way.

I posted also here today morning: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/cc68b10c-4e1c-4344-9f00-710185d4b1b0 If I get an answer, I will post it here too.

Thank you so much (in advance), Totti

Suhaib Janjua
  • 3,538
  • 16
  • 59
  • 73
TottiW
  • 82
  • 7

2 Answers2

1

You should use the AttachedCommandBehavior library. It will allow you to bind multiple commands to the same control:

<Button Grid.Row="3" x:Name="TestButtonObj" Content="asdlknm">
  <local:CommandBehaviorCollection.Behaviors>
    <local:BehaviorBinding Event="MouseLeftButtonDown" Action="{Binding SetPttDeactivatedCommand}" />
    <local:BehaviorBinding Event="MouseLeftButtonUp" Command="{Binding SetPttActivatedCommand}" />
    ...
  </local:CommandBehaviorCollection.Behaviors>
</Button>
Julien Poulin
  • 12,737
  • 10
  • 51
  • 76
  • Thanks, looks good. Hopefully I have tomorrow the time to investigate a little bit more... BTW I saw no license info: Can we use it in a commercial app? – TottiW Jul 28 '09 at 19:56
  • I don't think there is a license. The author says: "This code was not tested a lot so expect bugs etc... If you want to use this in production code you are doing so at your own risk" – Julien Poulin Jul 28 '09 at 20:44
  • I don't know what is your ultimate goal, but if you need to execute an action for as long as the button is pressed, you might want to take a look at the ReapeatButton, it fires a click event continuously, as long as it is pressed. – Julien Poulin Jul 30 '09 at 07:28
  • Constant event firing is not useful in our scenario... but thanks. Still no time to test it, but tomorrow I will know more – TottiW Jul 30 '09 at 14:40
  • I tried it and it works perfectly! You gain more command flexibility and you can also bind the event dynamically - NICE! Thanks Julien :) – TottiW Aug 14 '09 at 14:43
0

Did you try to set the name in the Binding?

<Setter Property="Command" Value="{Binding ElementName=TestButtonObj, Path=SetPttDeactivatedCommand}" />
Kenan E. K.
  • 13,955
  • 3
  • 43
  • 48
  • Now I tried it - but without success :( Doesn`t even stop at the breakpoint of: public ICommand SetPttDeactivetedCommant where the DelegateCommand should be instantiated – TottiW Jul 27 '09 at 21:04
  • Check if the Binding is Two-way. Other than that, maybe setters don't work with named elements outside of the template... – Kenan E. K. Jul 27 '09 at 21:54
  • The TwoWay binding was not the problem. But anyway, thanks for trying to help – TottiW Jul 28 '09 at 19:54