【发布时间】: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