【问题标题】:How to fire Click-Event in Templated-Button如何在模板按钮中触发点击事件
【发布时间】:2017-05-13 10:06:14
【问题描述】:

我已经搜索过这个问题,但没有找到任何解决方案。

我在 Button.Template 中有一个带有图像的模板按钮。此按钮是 CustomControl 的一部分。

<Button x:Name="PART_DELETESEARCHBUTTON"
        Style="{StaticResource CustomDeleteButtonStyle}"
        Command="{x:Static local:CustomSearchControl.DeleteCommand}"
        Width="20" Height="20"
        Margin="0,0,5,0">
  <Button.Template>
      <ControlTemplate>
          <Image x:Name="PART_IMGDELETE" 
                 Source="{DynamicResource _Av_PinClose_Dark}"
                 Cursor="Hand"
                 Margin="2"/>
          <ControlTemplate.Triggers>
              <Trigger Property="IsMouseOver" Value="True">
                  <Setter Property="Background" Value="Red"/>
              </Trigger>
          </ControlTemplate.Triggers>
      </ControlTemplate>
  </Button.Template>
          

在 CustomControl 的类中,实现了按钮的命令:

static CustomSearchControl()
{
    DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomSearchControl),
        new FrameworkPropertyMetadata(typeof(CustomSearchControl)));

    CommandManager.RegisterClassCommandBinding(typeof(CustomSearchControl),
                new CommandBinding(CustomSearchControl.DeleteCommand, C_DeleteCommand));
}

public override void OnApplyTemplate()
{
    base.OnApplyTemplate();
}

static void C_DeleteCommand(object sender, ExecutedRoutedEventArgs e)
{
    CustomSearchControl mycontrol = sender as CustomSearchControl;
    mycontrol.SearchText = "";
}
public static readonly ICommand DeleteCommand = new RoutedUICommand("DeleteCommand", "DeleteCommand",
                                    typeof(CustomSearchControl),
                                            new InputGestureCollection(new InputGesture[] { new KeyGesture(Key.Enter), new MouseGesture(MouseAction.LeftClick) }));

现在,如果鼠标单击按钮(图像),则不会触发命令。删除带有图像的 Button.Template 时,一切正常。

如何将 Templated.ButtonImage 上的 MouseClick 绑定到 Command 上?

或者有没有其他方法可以解决这个问题?

其次:DeleteCommand 清除此 CustomControl 中的 TextBox。那行得通,但是在清除之后,文本框失去了焦点。点击按钮后TextBox再次获得焦点怎么办???触发左右???

【问题讨论】:

    标签: c# wpf command custom-controls


    【解决方案1】:

    我无法重现命令未执行的问题。它对我来说很好。

    对于焦点问题,单击按钮时获得焦点。您必须明确地将焦点设置回文本框。这很容易。如果模板中没有名称,则给它一个;我叫我的“PART_SearchTextBox”,但你可以用你自己的名字代替。

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
    
        _PART_SearchTextBox = (TextBox)GetTemplateChild("PART_SearchTextBox");
    }
    
    public void ClearSearchText()
    {
        SearchText = "";
        _PART_SearchTextBox.Focus();
    }
    
    private TextBox _PART_SearchTextBox;
    
    static void C_DeleteCommand(object sender, ExecutedRoutedEventArgs e)
    {
        CustomSearchControl mycontrol = sender as CustomSearchControl;
        mycontrol.ClearSearchText();
    }
    

    在 CustomSearchControl 模板中,将文本框命名为 PART_SearchTextBox

    <TextBox 
        x:Name="PART_SearchTextBox"
        Text="{Binding SearchText, RelativeSource={RelativeSource TemplatedParent}}"
        ...etc...
        />
    

    【讨论】:

    • 感谢您的回答。你是对的,在一个小示例项目中,该命令对我有用,并且你的提示重点工作正常。
    • 我在一个大应用程序中使用了我的 CustomControl,但没有触发该命令。显然大应用程序中的其他控件存在其他问题。所以现在我必须搜索其他问题,也许其他一些控件或行为会覆盖我的 SearchCustomControl 中的某些内容。
    • @Jürgen 你在C_DeleteCommand() 中放了一个断点,它肯定不会被调用?
    • 我已经尝试在 C_DeleteCommand() 中使用断点,但绝对没有调用 :-( 我认为在我的大应用程序中还有一些其他 MouseBehaviors 会吃掉 MouseDown。这将是一个很好的搜索 :-(对我来说
    • 在我的大应用程序中,我在 CustomControl 中的 SearchTextProperty 遇到了同样的问题。属性只获取起始值:
    猜你喜欢
    • 1970-01-01
    • 2016-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多