Button:根据ClickMode值(按下/释放)绑定不同的DelegateCommand
经常在这里找到答案,但现在这是我第一次;)
我们将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用 AttachedCommandBehavior 库。 它将允许您将多个命令绑定到同一个控件:
You should use the AttachedCommandBehavior library. It will allow you to bind multiple commands to the same control:
您是否尝试在绑定中设置名称?
Did you try to set the name in the Binding?