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

发布于 2024-07-21 07:51:49 字数 1641 浏览 6 评论 0原文

我已经实现了此处找到的附加命令行为模式 并且它工作得很好,例如允许边框在 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>

代码隐藏:< /strong>

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

I've implemented the attached command behavior pattern found here and it works well to allow e.g. a Border to have a left- or right-click event that fires in the 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>

Code Behind:

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());
        }
    };
}

However, how do I attach two attached behaviors to one element, e.g. I want to do something like the following but it of course gives me an error:

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

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

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

发布评论

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

评论(2

高跟鞋的旋律 2024-07-28 07:51:49

您发送的链接包含该答案。 您可以使用 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>

The link you sent contains that very answer. You can use the CommandBehaviorCollection.Behaviors capabilities in ACB v2.

   <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>
灯角 2024-07-28 07:51:49

“就是这样,谢谢,有趣的是,我的 XAML 编辑器给了我错误“在‘CommandBehaviorCollection’类型中找不到可附加属性‘Behaviors’。”虽然我可以很好地运行和编译它,但这是为什么呢?

原因是允许命令行为集合(这是一个附加属性集合)的代码实际上是一种 XAML 漏洞。 您可以在这里阅读更多相关内容:
http://wekempf.spaces.live.com/ blog/cns!D18C3EC06EA971CF!468.entry?sa=276442122

"that was it, thanks, funny though that my XAML editor gives me the error "The attachable property 'Behaviors' was not found in type 'CommandBehaviorCollection'." although I can run and compile it fine, why is that?"

The reason is that the code that allows the command behavior collection (which is an attached property collection) is actualy sort of a XAML loophole. You can read more about that here:
http://wekempf.spaces.live.com/blog/cns!D18C3EC06EA971CF!468.entry?sa=276442122

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文