简体   繁体   中英

Button: Binding different DelegateCommands depending on the ClickMode value (Press / Release)

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

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>

您是否尝试在绑定中设置名称?

<Setter Property="Command" Value="{Binding ElementName=TestButtonObj, Path=SetPttDeactivatedCommand}" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM