在Silverlight中,双击按钮创建事件处理程序的功能并不直接支持。但是,您可以通过以下方法来实现这个功能:
然后在代码后台中定义事件处理程序:
private void myButton_Click(object sender, RoutedEventArgs e)
{
// 在这里编写您的事件处理代码
}
首先,在代码后台中定义一个附加属性:
public class DoubleClickBehavior
{
public static readonly DependencyProperty CommandProperty =
DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(DoubleClickBehavior), new PropertyMetadata(null, OnCommandChanged));
public static ICommand GetCommand(DependencyObject obj)
{
return (ICommand)obj.GetValue(CommandProperty);
}
public static void SetCommand(DependencyObject obj, ICommand value)
{
obj.SetValue(CommandProperty, value);
}
private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = d as Control;
if (control != null)
{
control.MouseLeftButtonDown += Control_MouseLeftButtonDown;
}
}
private static void Control_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var control = sender as Control;
if (control != null && e.ClickCount == 2)
{
var command = GetCommand(control);
if (command != null && command.CanExecute(null))
{
command.Execute(null);
}
}
}
}
然后在XAML代码中为按钮添加附加属性:
最后,在代码后台中定义命令:
public ICommand MyCommand => new RelayCommand(MyCommandExecute);
private void MyCommandExecute()
{
// 在这里编写您的事件处理代码
}
这样,您就可以在Silverlight中实现双击按钮创建事件处理程序的功能。
领取专属 10元无门槛券
手把手带您无忧上云