简体   繁体   English

XAML设置程序的命令属性

[英]XAML Setter Property to Command

I am trying to call a command when my mouse is over a toggle button. 我试图在鼠标悬停在切换按钮上方时调用命令。

I have the following code. 我有以下代码。

<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Cursor" Value="Hand"></Setter>
<Setter Property="Command" Value="{Binding Path=PushPinMouse}" />
</Trigger>

When I roll the mouse over, the hand shows. 当我将鼠标滑过时,手会显示。 But when i roll the mouse over it doesn't hit my PushPinMouse method.. Why's that? 但是,当我将鼠标滑过鼠标时,它没有命中我的PushPinMouse方法。为什么呢?

What you need here is attached behavior. 您需要的是附加行为。 I can recommend you to see this nice article : http://blogs.microsoft.co.il/blogs/tomershamam/archive/2009/04/14/wpf-commands-everywhere.aspx 我可以建议您看这篇漂亮的文章: http : //blogs.microsoft.co.il/blogs/tomershamam/archive/2009/04/14/wpf-commands-everywhere.aspx

Your code would looks like (with the Attached Commands library) : 您的代码看起来像(带有“附加命令”库):

<Style>
  <Setter Property="ts:CommandSource.Trigger">
    <Setter.Value>
      <ts:PropertyCommandTrigger Property="IsMouseOver" Value="true" Command="{Binding Path=PushPinMouse}"/>
    </Setter.Value>
  </Setter>
</Style>

This says : "When Mouse is over, executes the PushPinMouse command". 上面写着:“当鼠标结束时,执行PushPinMouse命令”。 If this is not the behavior that you need, maybe you can adapt this code ;) Like the others said, a button command is only executed when it is clicked, BUT this library can add commands to other events (whereas routed events or property changed events). 如果这不是您需要的行为,也许您可​​以修改此代码;)就像其他人所说的那样,仅在单击按钮命令时才执行按钮命令,但是此库可以向其他事件添加命令(而路由事件或属性已更改)事件)。

You will still need this trigger : 您仍然需要此触发器:

<Trigger Property="IsMouseOver" Value="True">
  <Setter Property="Cursor" Value="Hand"></Setter>
</Trigger>

Commands are executed by controls like ToggleButton when they are clicked. 单击诸如ToggleButton之类的控件时,将执行命令。 I don't have an idea about your scenario, but may be you can set IsChecked property in your setter, and have a property binded to IsChecked. 我对您的情况不了解,但是可能您可以在设置器中设置IsChecked属性,并将属性绑定到IsChecked。 Hope it does help. 希望对您有所帮助。

You are changing the command which the button will trigger, but it doesn't actually execute the button until you click it. 您正在更改按钮将触发的命令,但是直到您单击它,它才真正执行该按钮。 So in this case you would have to have the mouse over and click to execute the command. 因此,在这种情况下,您必须将鼠标悬停在上面并单击以执行命令。

I am assuming also that you have bound your command to the command function using a CommandBinding? 我还假设您已使用CommandBinding将命令绑定到命令功能?

命令未绑定,您需要分配命令用途Command = PushPinMouse

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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