【问题标题】:Command binding issue in CustomControl: CanExecute() fires, Execute() does notCustomControl 中的命令绑定问题:CanExecute() 触发,Execute() 不触发
【发布时间】:2016-07-21 15:56:05
【问题描述】:

我将 ControlTemplate 中的按钮命令绑定到 CustomControl 中的 Execute() 方法。我正在使用 RoutedCommand,CanExecute() 会触发,但 Execute() 永远不会触发。当 CustomControl 放置在主窗口中时,代码按预期工作。当它放在用户控件中时,我遇到了这个问题。我尝试了几种方法来连接按钮命令(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。在非静态构造函数中,我们将静态删除命令连接到 DeleteToken 方法:

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

最后是 CanDelete,它只是将 CanExecute 设置为 true:

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

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

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

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

【问题讨论】:

    标签: wpf binding custom-controls routedcommand


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 2022-08-19
      • 2017-04-18
      • 2014-12-31
      • 1970-01-01
      相关资源
      最近更新 更多