gpt4 book ai didi

wpf - 如何将两个附加行为附加到一个XAML元素上?

转载 作者:行者123 更新时间:2023-12-03 10:53:59 25 4
gpt4 key购买 nike

我已经实现了attached command behavior pattern found here,并且效果很好允许例如一个Border,具有在ViewModel中触发的左击或右击事件:

XAML:

<Border Background="Yellow" Width="350" Margin="0,0,10,0" Height="35" CornerRadius="2"
c:CommandBehavior.Event="MouseLeftButtonDown"
c:CommandBehavior.Command="{Binding PressedLeftButton}"
c:CommandBehavior.CommandParameter="MainBorder123">
<TextBlock Text="this is the click area"/>
</Border>

背后的代码:
public ICommand PressedLeftButton { get; private set; }

public MainViewModel()
{

Output = "original value";

PressedLeftButton = new SimpleCommand
{
ExecuteDelegate = parameterValue => {
Output = String.Format("left mouse button was pressed at {0} and sent the parameter value \"{1}\"", DateTime.Now.ToString(), parameterValue.ToString());
}
};
}

但是, 如何将两个附加行为附加到一个元素,例如我想做类似以下的事情,但它当然给我一个错误:
<Border Background="Yellow" Width="350" Margin="0,0,10,0" Height="35" CornerRadius="2"
c:CommandBehavior.Event="MouseLeftButtonDown"
c:CommandBehavior.Command="{Binding PressedLeftButton}"
c:CommandBehavior.CommandParameter="MainBorder123"
c:CommandBehavior.Event="MouseRightButtonDown"
c:CommandBehavior.Command="{Binding PressedRighttButton}"
c:CommandBehavior.CommandParameter="MainBorder123"
>

最佳答案

您发送的链接包含该答案。您可以在ACB v2中使用CommandBehaviorCollection.Behaviors功能。

   <Border Background="Yellow" Width="350" Margin="0,0,10,0" Height="35" CornerRadius="2" x:Name="test">
<local:CommandBehaviorCollection.Behaviors>
<local:BehaviorBinding Event="MouseLeftButtonDown" Action="{Binding DoSomething}" CommandParameter="An Action on MouseLeftButtonDown"/>
<local:BehaviorBinding Event="MouseRightButtonDown" Command="{Binding SomeCommand}" CommandParameter="A Command on MouseRightButtonDown"/>
</local:CommandBehaviorCollection.Behaviors>
<TextBlock Text="MouseDown on this border to execute the command"/>
</Border>

关于wpf - 如何将两个附加行为附加到一个XAML元素上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/926451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com