代码之家  ›  专栏  ›  技术社区  ›  Edward Tanguay

如何将两个附加的行为附加到一个xaml元素上?

  •  0
  • Edward Tanguay  · 技术社区  · 16 年前

    我已经实现了 attached command behavior pattern found here 而且它 工作良好 要允许(例如)边框具有在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"
            >
    
    2 回复  |  直到 12 年前
        1
  •  5
  •   Szymon Rozga    16 年前

    你发送的链接包含了这个答案。您可以在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>
    
        2
  •  0
  •   Quetzal Quintana    16 年前

    “正因为如此,谢谢,尽管我的XAML编辑器给了我一个错误”在“commandBehaviorCollection”类型中找不到可附加属性“behaviors”。“尽管我可以运行并编译它,但为什么会这样?”

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

    推荐文章