CustomControl中的命令绑定问题:CanExecute()触发,Execute()不触发

时间:2016-07-21 15:56:05

标签: wpf binding custom-controls routedcommand

我将ControlTemplate中的按钮命令绑定到CustomControl中的Execute()方法。我使用RoutedCommand,CanExecute()会触发,但Execute()永远不会触发。当CustomControl放在主窗口中时,代码按预期工作。当它被放置在Usercontrol中时,我遇到了这个问题。我已经尝试了几种方法来连接按钮Command(RelayCommand等),但似乎无法弄清楚错误是什么。任何帮助表示赞赏。

对于上下文,这是一个TokenizingTextBox控件 - Xceed开源版本的早期版本。该按钮用于从令牌列表中删除令牌。

TokenIten的完整样式(包含感兴趣的按钮):

    <Style TargetType="{x:Type local:TokenItem}">
      <Setter Property="Background" Value="#F3F7FD" />
      <Setter Property="BorderBrush" Value="#BBD8FB" />
      <Setter Property="BorderThickness" Value="1" />
      <Setter Property="Cursor" Value="Arrow" />
      <Setter Property="Padding" Value="2,1,1,1" />
      <Setter Property="Margin" Value="1,0" />
      <Setter Property="Template">
         <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:TokenItem}">
               <Border Background="{TemplateBinding Background}"
                       BorderBrush="{TemplateBinding BorderBrush}"
                       BorderThickness="{TemplateBinding BorderThickness}"
                       Padding="{TemplateBinding Padding}"
                       CornerRadius="0,0,5,5"
                       Margin="{TemplateBinding Margin}"
                       >
                        <StackPanel Orientation="Horizontal" Margin="1" x:Name="myRoot">
                            <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" />

                            <Button Margin="3,0,0,0" Cursor="Hand" 
                                    Command="{x:Static local:TokenizedTextBoxCommands.Delete}" CommandParameter="{TemplateBinding TokenKey}" 
                                    PresentationTraceSources.TraceLevel="High">
                        <!--<Button.Template>
                           <ControlTemplate TargetType="Button">
                              <ContentPresenter />
                           </ControlTemplate>
                        </Button.Template>-->
                        <Image Source="/Resources/delete8.png" Width="8" Height="8" />
                     </Button>
                  </StackPanel>
               </Border>
            </ControlTemplate>
         </Setter.Value>
      </Setter>
   </Style>    

静态命令:

  public static class TokenizedTextBoxCommands
  {
    private static RoutedCommand _deleteCommand = new RoutedCommand();

    public static RoutedCommand Delete => _deleteCommand;
  }

自定义控件继承自ItemsControl。在非静态构造函数中,我们将static delete命令连接到DeleteToken方法:

public TokenizedTextBox()
{
    CommandBindings.Add(new CommandBinding(TokenizedTextBoxCommands.Delete, DeleteToken, CanDelete));
}

最后将CanExecute设置为true的CanDelete:

    private void CanDelete(object sender, CanExecuteRoutedEventArgs canExecuteRoutedEventArgs)
    {
        canExecuteRoutedEventArgs.CanExecute = true;
    }

而DeleteToken - 功能被省略,签名在这里真的很重要:

    private void DeleteToken(object sender, ExecutedRoutedEventArgs e)
    {
        ...
    }

所以,希望这对任何有兴趣提供指导/建议的人来说都是足够的信息。感谢。

1 个答案:

答案 0 :(得分:0)

这里兴趣不大所以我通过Pluralsight聘请了导师。绑定是正确的,但CustomControl有一个RichTextBox捕获鼠标单击。我们使用针对Button的PreviewMouseDown的行为修复了该问题。