如何在模板化按钮中触发点击事件
How to fire Click-Event in Templated-Button
我搜索过这个问题,但没有找到任何解决方案。
我在 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 的 Class 中实现了按钮的命令:
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 失去了焦点。单击按钮后文本框再次获得焦点怎么办???触发还是这样???
我无法重现命令未执行的问题。它对我来说很好用。
焦点问题,按钮在点击时获得焦点。您必须明确地将焦点设置回文本框。这很简单。如果模板中没有名称,请给它一个;我叫我的 "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...
/>
我搜索过这个问题,但没有找到任何解决方案。
我在 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 的 Class 中实现了按钮的命令:
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 失去了焦点。单击按钮后文本框再次获得焦点怎么办???触发还是这样???
我无法重现命令未执行的问题。它对我来说很好用。
焦点问题,按钮在点击时获得焦点。您必须明确地将焦点设置回文本框。这很简单。如果模板中没有名称,请给它一个;我叫我的 "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...
/>