Button:根据ClickMode值(按下/释放)绑定不同的DelegateCommand

发布于 2024-07-27 23:36:45 字数 2258 浏览 5 评论 0原文

经常在这里找到答案,但现在这是我第一次;)

我们将 MVVM 模式与 DelegateCommands 结合使用。 所以通常我将命令绑定到按钮,如下所示:

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

当按下按钮和再次释放按钮时,我需要执行不同的命令。 我的想法如下:

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

问题是 TestButtonObj 未知。 好吧,我已经接受,我无法访问父对象。 如果没有 TargetName="TestButtonObj",它会编译,但不会执行该命令。 嗯……

好吧,我尝试了以下方法,但它不起作用……CommandBinding 不是依赖属性(希望你能让我走出这个迷宫)

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

此时我卡住了。 不知道是不是方法完全错了。 我读了一整天有关命令和绑定的文档,但我没有得到线索。 我希望,有人能给我指路。

今天早上我也在这里发帖: http://social. msdn.microsoft.com/Forums/en-US/wpf/thread/cc68b10c-4e1c-4344-9f00-710185d4b1b0 如果我得到答案,我也会在这里发布。

非常感谢(提前), 托蒂

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

喜你已久 2024-08-03 23:36:45

您应该使用 AttachedCommandBehavior 库。 它将允许您将多个命令绑定到同一个控件:

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

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>
清音悠歌 2024-08-03 23:36:45

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

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

Did you try to set the name in the Binding?

<Setter Property="Command" Value="{Binding ElementName=TestButtonObj, Path=SetPttDeactivatedCommand}" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文