Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在模板按钮中触发单击事件_C#_Wpf_Command_Custom Controls - Fatal编程技术网

C# 如何在模板按钮中触发单击事件

C# 如何在模板按钮中触发单击事件,c#,wpf,command,custom-controls,C#,Wpf,Command,Custom Controls,我已经搜索了这个问题,但没有找到任何解决方案 我有一个模板按钮,在Button.Template中有一个图像。此按钮是CustomControl的一部分 我无法重新创建命令未执行的问题。这对我来说很好 对于焦点问题,单击按钮可获得焦点。您必须显式地将焦点设置回文本框。那很容易。如果模板中没有名称,请给它一个名称;我叫我的“PART_SearchTextBox”,但你可以用自己的名字代替 public override void OnApplyTemplate() { base.OnAp

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

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


我无法重新创建命令未执行的问题。这对我来说很好

对于焦点问题,单击按钮可获得焦点。您必须显式地将焦点设置回文本框。那很容易。如果模板中没有名称,请给它一个名称;我叫我的“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\u SearchTextBox

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


谢谢您的回答。你是对的,在一个小例子Project中,该命令对我有效,并且使用你的提示focus工作正常。我在一个大型应用程序中使用了我的CustomControl,但没有触发该命令。显然,这个大应用程序中的其他控件还有其他问题。因此,现在我必须搜索另一个问题,可能是其他控件或行为覆盖了我的SearchCustomControl中的某些内容。@Jürgen您在
C_DeleteCommand()
中放置了断点,但它肯定没有被调用?我尝试过在C_DeleteCommand()中使用断点,但肯定没有调用:-(我认为在我的大应用程序中,还有其他一些鼠标行为会破坏鼠标向下。这将是一个伟大的搜索:-(对于我来说,在我的大应用程序中,我在CustomControl中的SearchTextProperty也有同样的问题。该属性只获得起始值: