简体   繁体   English

如何在模板化按钮中触发Click-Event

[英]How to fire Click-Event in Templated-Button

I have searched for this issue but didn't found any Solutions. 我已经搜索了此问题,但未找到任何解决方案。

I have a Templated-Button with an Image in Button.Template. 我在Button.Template中有一个带有图像的Templateed-Button。 This Button is Part of a CustomControl. 此按钮是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> 

In Class of the CustomControl the Command for the Button is implemented: 在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) })); 

Now, if MouseClick on Button (Image) the Command isn't fired. 现在,如果在按钮(图像)上单击鼠标,则不会触发该命令。 When removing Button.Template with Image, all works fine. 当删除带有图像的Button.Template时,一切正常。

How can the MouseClick on the Templated.ButtonImage binded to the Command? 如何在Templated.ButtonImage上的MouseClick绑定到命令?

Or is there annother way to solve this? 还是有其他解决方法?

And secondly: The DeleteCommand clears a TextBox in this CustomControl. 其次:DeleteCommand清除此CustomControl中的TextBox。 That works, but after Clearing, the TextBox lost the Focus. 可以,但是在清除之后,文本框失去了焦点。 What is to do that the TextBox gets the Focus again after Click on Button??? 在单击按钮后,TextBox再次获得焦点是做什么的??? Trigger or so??? 触发左右???

I can't recreate the problem with the command not executing. 我无法通过命令未执行来重新创建问题。 It's working fine for me. 对我来说很好。

For the focus issue, the button gets focus when it's clicked. 对于焦点问题,单击该按钮即可获得焦点。 You'll have to explicitly set focus back to the textbox. 您必须将焦点明确设置回文本框。 That's easy. 这很容易。 If it doesn't have a name in the template, give it one; 如果模板中没有名称,请给它一个名称;否则,给它一个名称。 I called mine "PART_SearchTextBox" but you can substitute your own name. 我称我为“ 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();
}

And in the CustomSearchControl template, name the textbox PART_SearchTextBox : 然后在CustomSearchControl模板中,将文本框命名为PART_SearchTextBox

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM