views:

503

answers:

2

Hey atAll!

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

A: 

Did you try to set the name in the Binding?

<Setter Property="Command" Value="{Binding ElementName=TestButtonObj, Path=SetPttDeactivatedCommand}" />
kek444
Now I tried it - but without success :(Doesn`t even stop at the breakpoint of:public ICommand SetPttDeactivetedCommantwhere the DelegateCommand should be instantiated
TottiW
Check if the Binding is Two-way. Other than that, maybe setters don't work with named elements outside of the template...
kek444
The TwoWay binding was not the problem. But anyway, thanks for trying to help
TottiW
A: 

You should use the AttachedCommandBahavior 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
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
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
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
Constant event firing is not useful in our scenario... but thanks. Still no time to test it, but tomorrow I will know more
TottiW
I tried it and it works perfectly! You gain more command flexibility and you can also bind the event dynamically - NICE!Thanks Julien :)
TottiW