我制作了一个自定义按钮,将命令绑定到(自定义,路由)IsPressedChanged
事件,以便在按下按钮和释放按钮时执行命令:
<local:CustomButton xmlns:i="http://schemas.microsoft.com/xaml/behaviors" x:Name="MyButton">
<i:Interaction.Triggers>
<i:EventTrigger EventName="CustomIsPressedChanged">
<i:InvokeCommandAction Command="{Binding Path=SomeCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</local:CustomButton>
使用自定义按钮实现:
public partial class CustomButton : Button
{
/* Register a custom routed event using the bubble routing strategy. */
public static readonly RoutedEvent CustomIsPressedChangedEvent = EventManager.RegisterRoutedEvent(
name: "CustomIsPressedChanged",
routingStrategy: RoutingStrategy.Bubble,
handlerType: typeof(RoutedEventHandler),
ownerType: typeof(CustomButton));
/* Provide CLR accessors for assigning an event handler. */
public event RoutedEventHandler CustomIsPressedChanged
{
add { AddHandler(CustomIsPressedChangedEvent, value); }
remove { RemoveHandler(CustomIsPressedChangedEvent, value); }
}
public CustomButton() { InitializeComponent(); }
/* Custom Event handling of the IsPressedChanged event */
protected override void OnIsPressedChanged(System.Windows.DependencyPropertyChangedEventArgs e)
{
/* Call the base class OnIsPressedChanged() method so IsPressedChanged event subscribers are notified. */
base.OnIsPressedChanged(e);
/* Raise custom event */
RaiseEvent(new RoutedEventArgs(routedEvent: CustomIsPressedChangedEvent));
}
}
这本应该是完美的.
And now comes the Problem:
当我try 将IsPressed
属性的值传播到命令时,如下所示:
<i:InvokeCommandAction Command="{Binding Path=SomeCommand}"
CommandParameter="{Binding ElementName=MyButton, Path=IsPressed}"/>
传播的值(似乎)始终是旧值IsPressed
.当我按下按钮时,调用的命令参数为false,当我松开按钮时,参数为true.But当我在事件处理程序CustomButton.OnIsPressedChanged()
中判断值IsPressed
时,它表示预期的新值.
My Question is:我应该如何传播IsPressed
的值以获得正确的值?是否保证总是使用旧值调用该命令?在这种情况下,我可以简单地反转值,但这对我来说似乎有点可疑,我真的不想这样做,除非我知道它将始终产生正确的结果.